开发者

How to pass an int value from one activity to other

开发者 https://www.devze.com 2023-04-09 13:53 出处:网络
can anyone help what is the problem in my code Activiy 1: int view=1; TabFunctionality.setFirstTabFunctionality(TabFunctionality.FIRST_TAB, 1);

can anyone help what is the problem in my code

Activiy 1:

int view=1;
TabFunctionality.setFirstTabFunctionality(TabFunctionality.FIRST_TAB, 1);
Intent intent = new Intent(AdultTeeth.this, MainScreen.class);
Bundle b = new Bundle();
b.putInt("TEXT", view);                 
intent.putExtras(b);    
startActivityForResult(intent, TEETH_VI开发者_高级运维EW);
finish();

Activity 2:

Bundle b = this.getIntent().getExtras(); int view=b.getInt("TEXT");


You can directly use putExtra also.

Activity 1

Intent intent = new Intent(AdultTeeth.this, MainScreen.class);
intent.putExtra("int_value", int_variable);
startActivity(intent);

Activity 2

Intent intent = getIntent();
int temp = intent.getIntExtra("int_value", 0); // here 0 is the default value


Passactivity:

 Intent i = new Intent(view.getContext(), Passactivity.class);        
 i.putExtra("font",selected_font);
 startActivity(i);

Receving activity

private int my_size;

 Intent i = getIntent();
     my_size = i.getIntExtra("size",20); // 20  for default value.


The answers given are here aren't wrong but they are incomplete in my opinion. The best way to do this with validations is to make sure that the extra is taken from the previous Activity as well as the savedInstanceState which is the Bundle data received while starting the Activity and can be passed back to onCreate if the activity needs to be recreated (e.g., orientation change) so that you don't lose this prior information. If no data was supplied, savedInstanceState is null.

Sending data -

Intent intent = new Intent(context, MyActivity.class);
intent.putExtra("name", "Daenerys Targaryen");
intent.putExtra("number", "69");
startActivity(intent);

Receiving data -

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.myactivity);

    int no;
    String na;

    if(savedInstanceState == null){
        Bundle extras = getIntent().getExtras();

        if(extras != null){
            no = Integer.parseInt(extras.getString("number"));
            na = extras.getString("name");
        }

    }else{
        no = (int) savedInstanceState.getSerializable("number");
        na = (String) savedInstanceState.getSerializable("name");
    }

    // Other code
}


in first activity ::

intent1.putExtra("key",int_score);
startActivity(intent1);

second Activity ::

  Intent i1 = getIntent();  
  int temp = i1.getIntExtra("key",1);                                                                                                               int temp = i1.getIntExtra("tranningscore2", 1);


just pass it like a string and typecast it


use this code may its work.

intent.putExtra("Text", view);

activity 2:

int i = getIntent().getIntExtra("Text", 0);

where 0 is the default value.


in first activity

 Intent i = new Intent(name_class.this, name_class.class);
 i.putExtra("valu1",valu1);
 i.putExtra("value2", valu2);

second Activity ::

    Bundle bundle = this.getIntent().getExtras();
    String valu1 =bundle.getString("value1");
    String value2 = bundle.getString("value2");
0

精彩评论

暂无评论...
验证码 换一张
取 消