简体   繁体   English

所有活动实例的Android onSaveInstanceState

[英]Android onSaveInstanceState for all instances of activity

After reading the documention it seems that onSaveInstanceStaate works per instance (as the name suggests). 阅读该文档后,似乎onSaveInstanceStaate每个实例都可以工作(顾名思义)。 I am just wondering what the preffered method of storing data is so that it is available for all instances of that activity? 我只是想知道什么是存储数据的首选方法,以便它可用于该活动的所有实例?

As MaciejGorski mentioned in his comment, there are different levels of data storage available in Android : 正如MaciejGorski在评论中提到的,Android中存在不同级别的数据存储

  1. Shared preferences 共享的首选项
  2. Internal storage 内部存储器
  3. External storage 外置储存
  4. SQLite database SQLite数据库
  5. Network 网络

From personal experience, the lower you go down this list, the more complicated your implementation will become. 从个人经验来看,您在此列表中花的越少,实现的难度就越大。 Thus, if you are simply trying to save simple data for your app to be shared among different instances of an activity (or of multiple activities), shared preferences are certainly the way to go. 因此,如果您只是想保存简单的数据以便在一个活动(或多个活动)的不同实例之间共享您的应用程序,那么共享首选项无疑是您的最佳选择。 You can even create private shared preferences , which only your app can access. 您甚至可以创建私有共享首选项 ,只有您的应用可以访问。

In any case, check out this SO answer for how to implement them: How to use SharedPreferences in Android to store, fetch and edit values 无论如何,请查看此SO答案以了解如何实现它们: 如何在Android中使用SharedPreferences来存储,获取和编辑值

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

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