简体   繁体   English

在应用运行时保留HashMap数据 - Android

[英]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的生命周期是完全不同的:

  • onDestroy() will not be called (but onDetach() still will be, because the fragment is being detached from its current activity). onDestroy()不会被调用(但是onDetach()仍然会被调用,因为片段正在与其当前活动分离。
  • onCreate (Bundle) will not be called since the fragment is not being re-created. 因为片段没有被重新创建,所以不会调用onCreate (Bundle)。
  • onAttach (Activity) and onActivityCreated(Bundle) will still be called. onAttach (Activity)和onActivityCreated(Bundle)仍将被调用。

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.

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