[英]Keep HashMap data while app is running - Android
I have a HashMap which contains data that is relevant to a specific Activity. 我有一个HashMap,其中包含与特定Activity相关的数据。 The data should be fetched from a server and is quite big so I dont want to fetch it inside that certain Activity.
数据应该从服务器获取并且非常大,所以我不想在某个Activity内部获取它。 Instead, I am fetching all the data in the main activity, into a custom class and then I create a HashMap for holding all of the objects and save it in my Application class.
相反,我将主活动中的所有数据提取到一个自定义类中,然后创建一个HashMap来保存所有对象并将其保存在我的Application类中。
When the user goes into the other activity, the data is ready to go without any need to wait, by calling the HashMap I created earlier from the Application class. 当用户进入另一个活动时,通过调用我之前从Application类创建的HashMap,数据准备就绪,无需等待。
It is all working fine except some times when the app is in the background for a long time, the data stored in the HashMap is being initialize by Android. 除了应用程序在后台很长一段时间,存储在HashMap中的数据正在由Android初始化时,一切正常。
I've read that storing objects in the Apllication class is bad and I wont be able to avoid this error, so my question is what is the right approach for doing that proccess? 我已经读过在Apllication类中存储对象很糟糕,我无法避免这个错误,所以我的问题是这个过程的正确方法是什么? I need a solution that will keep my HashMap object alive as long as there's an instance of my app.
我需要一个解决方案,只要有我的应用程序的实例,我的HashMap对象就会保持活动状态。
I have a HashMap which contains data that is relevant to a specific Activity.
If this is the case, why wouldn't you want to keep the HashMap
as an instance variable in your activity? 如果是这种情况,为什么不想将
HashMap
作为活动中的实例变量? Storing data for a specific Activity
in your Application
object isn't good object-oriented design. 在
Application
对象中存储特定Activity
数据不是良好的面向对象设计。
If you need to keep the data in the HashMap
when the Activity
is destroyed and created, you can save it in onSaveInstanceState()
. 如果在销毁和创建
Activity
时需要将数据保留在HashMap
,可以将其保存在onSaveInstanceState()
。
One of the available approaches is to use a "headless" retained Fragment
to persist the HashMap
data across Activity
recreation. 其中一种可用的方法是使用“无头”保留的
Fragment
来在Activity
娱乐中保留HashMap
数据。 According to android docs such Fragment
's lifecycle is quite different: 根据android文档 ,
Fragment
的生命周期是完全不同的:
Thus, when your Activity
dies from a configuration change and so on, your Fragment
does not. 因此,当您的
Activity
从配置更改等等死亡时,您的Fragment
不会。 When Activity
is recreated, it could request your retained Fragment
for a piece of "cached" data. 重新创建
Activity
,它可以请求保留的Fragment
以获取一段“缓存”数据。 Read this , if you like to do it this way. 阅读本 ,如果你喜欢做这种方式。
The other approach I could think of is to cache your data in a database or a file, but that would be an overkill, I guess. 我能想到的另一种方法是将数据缓存在数据库或文件中,但我想这可能是一种过度杀伤力。
The data should be fetched from a server and is quite big so I dont want to fetch it inside that certain Activity.
数据应该从服务器获取并且非常大,所以我不想在某个Activity内部获取它。
Mb better to use Sqlite for data storage purposes because android devices doesnt have unlimited resoureces? Mb更好地使用Sqlite进行数据存储,因为Android设备没有无限的资源?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.