简体   繁体   English

如何在活动之间共享大量数据?

[英]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. 我有一个MainActivity ,可以使用Retrofit从Web服务器中获取大量数据。 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. 早些时候,我使用静态Controller来跟踪数据,但是我一直听到很多关于静态容器的批评。 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) 我认为您应该考虑2种选择(取决于您要共享的数据量以及应用程序可以运行的设备类型)

  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 您可以在此处阅读有关此内容的更多信息: http : //www.helloandroid.com/tutorials/maintaining-global-application-state

  2. You can use persistence storage (ie sqlite/file). 您可以使用持久性存储(即sqlite /文件)。

There are trade-offs for each approach. 每种方法都需要权衡。 And which one will suit you best depends on your specific setup. 哪一种最适合您取决于您​​的特定设置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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