简体   繁体   English

Android如何在应用程序重新启动时保持静态数据?

[英]Android how to keep static data on application relaunch?

I am having an abstract class having all static objects and function to store some globel data of the application during the execution session. 我有一个抽象类,其中包含所有静态对象和函数,用于在执行会话期间存储应用程序的某些全局数据。

All data reset to null once I relaunch the application in ICS and above version with Systems's setting ALWAYS_FINISH_ACTIVITIES is set as true. 在系统设置为ALWAYS_FINISH_ACTIVITIES的ICS和更高版本中重新启动应用程序后,所有数据都将重置为null。

Whats the better way to mainitan data on application relaunch? 在应用程序重新启动时维护数据的更好方法是什么?

Regards, 问候,

Android IT Android IT

Editing my question: I know that Sharepreference can be a better option but I dont want to store data for multiple sessions of the application and data I am storing is huge. 编辑我的问题:我知道Sharepreference可能是一个更好的选择,但是我不想为应用程序的多个会话存储数据,并且我存储的数据非常庞大。 Regards, Android IT 此致Android IT

Each Android app is running in a dependent process. 每个Android应用程序都在一个依赖的进程中运行。 When an app become background and system need memory, the app's process may be killed by system. 当应用程序变为后台并且系统需要内存时,该应用程序的进程可能会被系统杀死。 In this situation, when app relaunch, static objects will lost. 在这种情况下,应用重新启动时,静态对象将丢失。

I think use SharedPreference to maintain data is a better way. 我认为使用SharedPreference维护数据是一种更好的方法。

将数据存储在本地存储上似乎是合适的: http : //developer.android.com/training/basics/data-storage/index.html

You can store the data local using a XML file. 您可以使用XML文件在本地存储数据。 You can read the XML File on startup with a DOM Reader 您可以在使用DOM Reader启动时读取XML文件

http://www.ibm.com/developerworks/library/x-android/ http://www.ibm.com/developerworks/library/x-android/

If you have lots of data you can use a SQLite Database 如果您有大量数据,则可以使用SQLite数据库

http://developer.android.com/training/basics/data-storage/databases.html http://developer.android.com/training/basics/data-storage/databases.html

If there are not too much data you can keep them in SharedPreferences 如果没有太多数据,则可以将其保留在SharedPreferences中

You access them with PreferenceManager.getDefaultSharedPreferences() 您可以使用PreferenceManager.getDefaultSharedPreferences()访问它们

Once application exit or destroy that will remove all data . 一旦应用程序退出或销毁,将删除所有数据。 if you want to access after relaunch you should save your data as per your requirement . 如果要在重新启动后访问,则应根据需要保存数据。 like local or web. 例如本地或网络。 its deepen you. 它加深了你。 right now you can store data in preference so it will useful after relaunch the application. 现在,您可以优先存储数据,这样在重新启动应用程序后将很有用。 I think this is better for you : How to use SharedPreferences in Android to store, fetch and edit values 我认为这对您更好: 如何在Android中使用SharedPreferences来存储,获取和编辑值

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

相关问题 Android:如何防止重新启动应用程序上的全局数据 - Android : How to prevent global data on Application relaunch 如果应用程序在Android中崩溃,如何以编程方式重新启动/重新启动它? - How to restart/relaunch application programatically if it crashes in Android? 重新启动应用程序时重新启动Android活动 - Restart Android activity on relaunch of application 当应用程序在 android 中从后台转到前台时如何重新启动应用程序? - How to relaunch application when app went to background to foreground in android? 被杀死后如何立即在android中重新启动应用程序? - How to relaunch a application immediately in android after being killed? 如何重新启动在Android中按下主屏幕按钮退出的应用程序? - How to relaunch the application which is exited by home button press in android? Android:在应用程序中保留静态内容的最佳方法 - Android: Best way to keep static content in application 如何在Espresso Android中为每个单独的测试用例重新启动应用程序 - How do i relaunch application from start for every individual test case in Espresso Android 如何在Android上使用通知点击意图中的新数据重新启动当前活动 - How to relaunch current activity with new data from notification click intent on Android 卸载并重新安装android应用程序后如何保留数据 - How to keep data after uninstall and reinstall android application
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM