简体   繁体   English

更新期间是否保留了 savedInstanceState?

[英]Is savedInstanceState kept during updates?

So, Activity.onCreate() takes a Bundle as a parameter, by default it's called savedInstanceState .因此, Activity.onCreate()Bundle作为参数,默认情况下它称为savedInstanceState Is the savedInstanceState kept when the app is updated to a newer version?当应用程序更新到较新版本时,是否保留savedInstanceState (let's just say through Google Play). (我们只是说通过 Google Play)。 Can I put some data in savedInstanceState , update the app to a new version, and still access the data?我可以将一些数据放入savedInstanceState ,将应用程序更新到新版本,并且仍然可以访问数据吗?

The documentation states:文件指出:

if the system destroys the activity due to system constraints (rather than normal app behavior), then although the actual Activity instance is gone, the system remembers that it existed such that if the user navigates back to it, the system creates a new instance of the activity using a set of saved data that describes the state of the activity when it was destroyed.如果系统由于系统限制(而不是正常的应用程序行为)而销毁了 Activity,那么尽管实际的 Activity 实例已经消失,系统会记住它存在,因此如果用户导航回它,系统会创建一个新的实例活动使用一组保存的数据来描述活动被销毁时的状态。 The saved data that the system uses to restore the previous state is called the "instance state" and is a collection of key-value pairs stored in a Bundle object.系统用于恢复先前状态的保存数据称为“实例状态”,是存储在 Bundle 对象中的键值对的集合。

But there is no reference about what happens if the application is killed while updating.但是没有关于如果应用程序在更新时被终止会发生什么的参考。

You should use SharedPreferences to persist information safely.您应该使用SharedPreferences来安全地保存信息。

Saved instance data is not preserved during application updates.应用程序更新期间不会保留已保存的实例数据。 This actually makes sense not only because of the data type potential incompatibility risk among different versions but also because your new binaries could simply be completely new application, sharing just packageId and signing certificate.这实际上是有道理的,不仅因为不同版本之间的数据类型潜在的不兼容风险,还因为您的新二进制文件可能只是全新的应用程序,仅共享packageId和签名证书。 That's why new version requires restart - after update user will start with fresh state as this is much safer than trying to restore the app to the state it was before update started (which can be simply impossible).这就是为什么新版本需要重新启动 - 更新后用户将以新状态开始,因为这比尝试将应用程序恢复到更新开始前的状态(这可能根本不可能)要安全得多。

So if you want data to remain unaffected by updates, database or shared preferences is much better choice.因此,如果您希望数据不受更新的影响,数据库或共享首选项是更好的选择。

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

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