简体   繁体   中英

Passing data through multiple activities in android

I have application in which I have three activities. First and second activity have listview. I need selected values from the first and second activity in order to display some data in the third activity. Which is the best way to do this? Is it better to pass Intent from first to second activity, then from second to third activity or to use SharedPrefenreces?

This depends on what you need. If you need the data to be persistent in the event that your app is destroyed, then you will probably want to save the data in SharedPreferences (will also depend on what type of data since SharedPrefs aren't limited to what they can store) or a DB.

If you aren't worried about it being persistent then passing the data through Intent.putExtra() is fine. You can also create a Bundle and pass the Bundle through the Intent and add the data as you go.

You also have the option of creating a separate class which implements Serializable . Then you can create and pass the class Object through your Intents . Here is a post about doing that

Yes this can be achieved in many ways using :

  • Intents
  • Shared Preferences
  • Sqlite

  1. Sqlite : You can use Sqlite to store data if the data is huge in first and second activity and retrieve the data in the third activity (But if data is less this is not a feasible solution)

  2. Shared Preferences : Here the data you have saved in the shared-Preferences is done in a xml file, so even if the app is stopped the data is retained. This is helpful to save the password and login details etc

  3. Intents : My opinion Passing data with intents is much better option in your case because size of the data passed between your first, second to third activity is less.


Finalizing :: Just put the data in bundle in first activity. Next get the data(Bundle) in second activity and add the data in second activity in addition to the data in the bundle received from first activity and pass that final bundle from second activity to the third activity.

In my opinion, it would be easiest to create an ArrayManager class, in which your variables are static. This way you can create methods to select and resize your arrays in the ArrayManager class. The only case in which this is bad is if you must hold references to Views, Contexts, or Activities (this will create a memory leak). In that case, codeMagic's solution is ideal.

In your case, if you need to pass same data from first activity to second activity then third activity & your data is small then I suggest use Shared Preferences .

If data passed from first activity to second activity is not same data of second activity to third activity then simply use intent.

There is a tricks. If your second activity always need data from first activity to operate then when you want to go back third activity to second activity then you will get stuck because from third activity you can not get first activity result. In that case saving data i mean Shared Preferences or Sqlite is best.

All above those scenario you need to find your own what is best to use for your app.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM