Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass multiple variable with putExtra

I have some variables that I want to pass to my next activity, but I can't figure a way to do that.

My variables are:

JSONObject jsonObj = jsonArray.getJSONObject(i);
String propId = jsonObj.getString("id");
Log.i("Value id", propId);
String propCity = jsonObj.getString("city");
Log.i("Value city", propCity);
String propPlace = jsonObj.getString("place");
Log.i("Value place", propPlace);
String propStation = jsonObj.getString("station");
Log.i("Value station", propStation);

and the code I used to get them is:

Bundle extras = new Bundle();
extras.putString("id", propId);
extras.putString("city", propCity);
extras.putString("place", propPlace);
extras.putString("station", propStation);

Can anyone help me with this please ?

like image 228
user3507621 Avatar asked Jan 10 '23 20:01

user3507621


1 Answers

The code you posted is to write them to the Bundle, after you write in the Bundle use .putExtras() to put your bundle in your Intent.

intent.putExtras(bundle);

Example:

Intent intent = new Intent(this, YourClass.class);
Bundle extras = new Bundle();
extras.putString("id", propId);
extras.putString("city", propCity);
extras.putString("place", propPlace);
extras.putString("station", propStation);
intent.putExtras(bundle);
startActivity(intent);

To read it in your activity use getExtras() to get the Bundle you passed to it and then use getString/getXXX.

Anyway, you can avoid the creation of the Bundle and use directly the set methods of Intent which works in the same way.

So it would be:

Intent intent = new Intent(this, YourClass.class);
intent.putExtra("id", propId);
intent.putExtra("city", propCity);
intent.putExtra("place", propPlace);
intent.putExtra("station", propStation);
startActivity(intent);
like image 168
Marco Acierno Avatar answered Jan 17 '23 16:01

Marco Acierno