How to pass int value from one activity to another

can anyone help what the problem is 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_VIEW); finish(); 

Action 2:

 Bundle b = this.getIntent().getExtras(); int view=b.getInt("TEXT"); 
+6
source share
7 answers

You can also use putExtra directly.

Action 1

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

Action 2

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

Passactivity:

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

Receiving activity

private int my_size;

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

The answers here are not mistaken, but they are incomplete, in my opinion. The best way to do this with validations is to make sure that additional data is taken from the previous action, as well as the saved InstanceState, which is the Bundle data received when the Activity started, and can be returned to onCreate if you need to activate the activity (for example, changing the orientation), so that you do not lose this preliminary information. If there was no data, savedInstanceState is NULL.

Sending data -

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

Data Acquisition -

 @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 } 
+1
source

in the 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); 
0
source

use this code can its work.

 intent.putExtra("Text", view); 

activity 2:

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

where 0 is the default value.

0
source

in the first action

 'Intent i = new Intent(name_class.this, name_class.class); 

i.putExtra ("valu1", valu1); i.putExtra ("value2", value2);

second activity ::

Bundle Package = this.getIntent (). getExtras ();

  String valu1 =bundle.getString("value1"); String value2 = bundle.getString("value2"); 
0
source

just pass it as a string and type it

0
source

Source: https://habr.com/ru/post/898432/


All Articles