2

I have 5 Activities in my App. Each Activity having some data. I need all previous Activity data in last Activity. All previous Activity is a kind of Form. User fill the data and move to next Activity and last Activity displays all previous Activity's data.

3 Answers3

1

Option 1:

If you want to add data for every activity and carryforward it to next activity then it's better to pass data with bundle.

Bundles are better in a scenario, when data is comparatively small and where you just want to pass data to next activity and don't want to store the data for future.

Option 2:

You can use Sharedprefrences. Better to use when data is to be stored for future.

Option 3:

If you have more records and you want it to be in a structured manner, and to be stored for future then Sqlitedatabase is obvious.

Above to options are in case if you want to use your data to use very frequently and it is not structured.

Gru
  • 2,686
  • 21
  • 29
1

There are several ways to do this.

  • Use of Intents and save the data like putString(...):
    Which isn't really nice because you have to temporarilly save the data of each Intent in the Activity and move them further with the next Intent.

  • Use a class for your data and pass it through the Activities per Intent:
    This way you pass the instance of the class to the Activities to write/read your data. But this class needs the Parcelable / Serializable interface

  • Use of a static class:
    You can access this class in every Activity and write/read like you want.

  • Use a Bundle with the Intent:
    Save all your data in a Bundle and pass it to the Activities.

  • Think about your design and use Fragments:
    Have one Activity with 5 Fragments. Every Fragment saves/reads the data from the Activity.

Best approach would be to use a Bundle.

Steve Benett
  • 12,843
  • 7
  • 59
  • 79
0

You can use Intent

FirstActivity

Intent intent = new Intent(FirstActivity.this,SecondActivity.class);
intent.putExtra("key_value", string); 
startActivity(intent);

SecondActivity

String text = getIntent().getStringExtra("key_value");

Or if you need the data to stay longer, and you can reuse it after the application is killed, you can use SharedPrefrences

Place the data in some Activity

 SharedPreferences.Editor editor = getPreferences(MODE_PRIVATE).edit();
 editor.putString("ValueOne", "SomeValue" );
 editor.commit();

Retrieve data from shared preference in any Activity

SharedPreferences prefs = getPreferences(MODE_PRIVATE); 
String prefString = prefs.getString("ValueOne", null);
Naskov
  • 4,121
  • 5
  • 38
  • 62