Reputation: 15552
I have a bundle that I want to pass through more than one activity.
Consider this example. I have activity1, activity2 and activity3. Activity1 goes to Activity2. Activity2 goes to Activity3. I want to get information from activity1 to activity3
My code would be
Intent intent = new Intent(v.getContext(), Activity2.class);
intent.putExtra(KEY, "Straight There");
startActivity(intent);
and then I would have to do this in Activity2
Bundle extras = getIntent().getExtras();
if (extras != null)
text = extras.getString(KEY);
Intent intent = new Intent(v.getContext(), Activity3.class);
intent.putExtra(KEY, text);
startActivity(intent);
Is there anyway I can pass the whole bundle through the activity without having to parse the keys and rebundle?
Thanks in advance
Upvotes: 0
Views: 55
Reputation: 195
I think you can use SharedPreferences to avoid passing data activity by activity,like this: in your first activity,save data by SharedPreferences:
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
SharedPreferences.Editor editor = settings.edit();
editor.putString(KEY, "Straight There");
in your third activity,get data by SharedPreferences:
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
String result = settings.getString(KEY, null);
more info about SharedPreferences,see:Storage Options
Upvotes: 2
Reputation: 2026
you can use putExtras(Intent src) to pull all the extras out of one Intent
and put them into another.
Intent intent = new Intent(v.getContext(), Activity3.class);
intent.putExtras(getIntent()); // get all the extras out of the current Activities Intent
startActivity(intent);
Upvotes: 2