Android的保持活动的背景背景、Android

2023-09-12 05:34:49 作者:不忘初心方得始终

我有多个活动的应用程序 - MainActivity,CpuActivity,等我CpuActivity有有 savedInstanceState 片段。每当我preSS背部或导航到MainActivity的CpuActivity被杀害和 savedInstanceState 丢失。是否有可能继续保存的数据/活动的背景或者我需要使用某种形式的临时存储(如共享preferences)?

I have a multiple activity app - MainActivity, CpuActivity, etc. My CpuActivity has fragments that have savedInstanceState. Whenever I press back or navigate up to the MainActivity the CpuActivity is killed and the savedInstanceState is lost. Is it possible to keep the saved data/activity in the background or do I need to use some kind of temporary storage (e.g. SharedPreferences)?

中的数据只能存放,直到整个应用程序被杀害,因此没有任何系统比共享preferences好?

The data should only be stored until the whole app is killed, so is there any system better than SharedPreferences?

推荐答案

一个简单的解决方案是将数据存储在您的应用程序实例。要做到这一点,你必须指定在清单中的应用程序实现(安卓应用程序中的变量名称属性)。您可以使用 getApplication()方法

A simple solution is to store the data in your application instance. To do that you have to specify an Application implementation in the manifest (the android:name attribute in the application tag). You can get the application using the getApplication() method.