简体   繁体   English

应用升级期间是否清除了Android活动instanceState?

[英]Is the Android activity instanceState cleaned during app upgrade?

In the instanceState of an activity we store a Serializable (bundle.putSerializable). 在活动的instanceState中,我们存储了Serializable(bundle.putSerializable)。 In a crash report from the Play Store we saw the following stack trace: 在Play商店的崩溃报告中,我们看到了以下堆栈跟踪:

Caused by: java.lang.ClassNotFoundException: o.ণ
at java.lang.Class.classForName(Class.java)
at java.lang.Class.forName(Class.java:308)
at android.os.Parcel$2.resolveClass(Parcel.java:2373)
at java.io.ObjectInputStream.readNewClassDesc(ObjectInputStream.java:1641)
at java.io.ObjectInputStream.readClassDesc(ObjectInputStream.java:657)
at java.io.ObjectInputStream.readNewObject(ObjectInputStream.java:1782)
at java.io.ObjectInputStream.readNonPrimitiveContent(ObjectInputStream.java:761)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:1983)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:1940)
at android.os.Parcel.readSerializable(Parcel.java:2381)

Our code is obfuscated by DexGuard. 我们的代码被DexGuard混淆了。 It looks like Android is able to serialize the class but not able to deserialize the class. 看起来Android能够序列化类但不能反序列化类。

The only reason we currently can think of is an upgrade of the app (via de Play Store). 我们目前可以想到的唯一原因是应用程序的升级(通过de Play商店)。 Between the version the class is changed (because of Dexguard) making a deserialization impossible. 在版本之间,类被更改(因为Dexguard)使反序列化成为不可能。

So, my question is: does Android clean all instanceState of an app during the upgrade of an App or not? 所以,我的问题是:在升级应用程序期间,Android是否清除应用程序的所有instanceState?

As far as I know, the only condition under which the system restores the state of an Activity when it's completely destroyed is when the system had to destroy that Activity to recover system memory. 据我所知,系统在完全销毁时恢复活动状态的唯一条件是系统必须销毁该活动以恢复系统内存。 In all other cases (force process to stop, reinstallation of the app, restart of the device) the state is lost. 在所有其他情况下(强制停止进程,重新安装应用程序,重新启动设备)状态将丢失。

About the reinstallation, you have to consider that when you install a new version of the app, you have probably changed or removed one or more activities, and the state of the old activities cannot probably match the state of the new ones. 关于重新安装,您必须考虑在安装新版本的应用程序时,您可能已经更改或删除了一个或多个活动,并且旧活动的状态可能无法与新活动的状态相匹配。 Suppose for example that in a single Activity app, you have a TextView with id "@+id/my_text". 例如,假设在单个Activity应用程序中,您有一个ID为“@ + id / my_text”的TextView。 After few days, you decide to remove the TextView and assign the id "my_text" to another View - for example a Spinner: how could the system restore the text of the TextView in a Spinner? 几天后,您决定删除TextView并将id“my_text”分配给另一个View - 例如Spinner:系统如何在Spinner中恢复TextView的文本?

You shouldn't include custom classes in the saved instanceState if there is any chance that those classes will change (even if it's only DexGuard shuffling names around). 如果这些类有可能改变(即使它只是DexGuard改组名称),你不应该在已保存的instanceState中包含自定义类。 The system persists some information about recent activities and may try to reuse it across upgrades. 系统会保留有关最近活动的一些信息,并可能尝试在升级过程中重复使用它。

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

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