简体   繁体   中英

How to share a large amount of data between activities?

I have a MainActivity that fetches a large-ish amount of data from a web server using Retrofit. This data is shown in a recycler view.

Now, I want to have another activity which works on the exact same data set. What's the most efficient way to do this? Earlier I have done this using a static Controller that keeps track of the data, but I keep hearing a lot of critique about static containers like that. Passing the data in the intents is not optimal, since the data is rather complicated and there's so much of it.

Optimally, I'd like to be able to modify the data set in either of the activities so that the changes are reflected to the other activity as well.

I think you should consider 2 options (depending on the amount of data you would like to share, and the type of devices your app is suppose to run on)

  1. You can extended the application class to include global data between your activities. You can read more about it here: http://www.helloandroid.com/tutorials/maintaining-global-application-state

  2. You can use persistence storage (ie sqlite/file).

There are trade-offs for each approach. And which one will suit you best depends on your specific setup.

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