2

I would like to pass data from one activity to another.If it is two or three activities we can send data via intent.suppose more number of activities are present (approximately 20).how can i pass data from first activity to last activity?

i want to go Activity A-->B-->C-->D-->......Y-->Z

if we send data via intent(put Extra) that is worst method.

is there any other way to send data?

thanks in advance

3 Answers3

1

I would use SharedPreferences for this.

This will be easier because, we can change it anywhere in any activity and access them as needed. And we don't need to pass on each and every activity transition.

Simple example: To set value in shared preference

SharedPreferences.Editor editor = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE).edit();
 editor.putString("name", "Nabin");
 editor.putInt("idName", 12);
 editor.commit();

And retrieve as

SharedPreferences prefs = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE); 
String restoredText = prefs.getString("text", null);
if (restoredText != null) {
  String name = prefs.getString("name", "No name defined");//"No name defined" is the default value.
  int idName = prefs.getInt("idName", 0); //0 is the default value.
}

You can refer here more about it.

Community
  • 1
  • 1
Nabin
  • 9,681
  • 7
  • 58
  • 91
0

If you need some data to multiple activities, Just save data into SharedPreference and you will be able to access to all activities. Here is full tutorial.

Save Data

    // Create object of SharedPreferences.
     SharedPreferences sharedPref= getSharedPreferences("mypref", 0);
     SharedPreferences.Editor editor= sharedPref.edit();
    //put your value
     editor.putString("name", strName);
     editor.putString("pwd", strPass);
     editor.commit();  //commits your edits

Retrieve Data

 SharedPreferences sharedPref= getSharedPreferences("mypref", 0);
 String name = sharedPref.getString("name", "");
 String password = sharedPref.getString("pwd", "");
MAC
  • 15,363
  • 8
  • 51
  • 92
0

If it's not a must case to use activities, you can change activities to fragments, attach them to same activity, cache your data in activity and get it from fragments.

savepopulation
  • 10,674
  • 4
  • 47
  • 68