我有一个Activity
我已经实施onSaveInstanceState()
并onRestoreInstanceState()
正确维护Activity
状态的地方。如果我旋转设备,一切都会成功并Activity
保持状态。
当我将应用程序置于后台并使用任务杀手杀死它或在设备的开发人员选项中打开“不保留活动”选项时,会出现问题。在这种情况下,当我重新启动时Activity
,我得到以下异常
09-05 16:04:36.567: E/AndroidRuntime(1976): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.vss.example/com.vss.example.ExampleActivity}: java.lang.RuntimeException: Parcel android.os.Parcel@b389dec8: Unmarshalling unknown type code 6619245 at offset 800
09-05 16:04:36.567: E/AndroidRuntime(1976): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
09-05 16:04:36.567: E/AndroidRuntime(1976): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
09-05 16:04:36.567: E/AndroidRuntime(1976): at android.app.ActivityThread.access$600(ActivityThread.java:130)
09-05 16:04:36.567: E/AndroidRuntime(1976): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
09-05 16:04:36.567: E/AndroidRuntime(1976): at android.os.Handler.dispatchMessage(Handler.java:99)
09-05 16:04:36.567: E/AndroidRuntime(1976): at android.os.Looper.loop(Looper.java:137)
09-05 16:04:36.567: E/AndroidRuntime(1976): at android.app.ActivityThread.main(ActivityThread.java:4745)
09-05 16:04:36.567: E/AndroidRuntime(1976): at java.lang.reflect.Method.invokeNative(Native Method)
09-05 16:04:36.567: E/AndroidRuntime(1976): at java.lang.reflect.Method.invoke(Method.java:511)
09-05 16:04:36.567: E/AndroidRuntime(1976): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
09-05 16:04:36.567: E/AndroidRuntime(1976): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
09-05 16:04:36.567: E/AndroidRuntime(1976): at dalvik.system.NativeStart.main(Native Method)
09-05 16:04:36.567: E/AndroidRuntime(1976): Caused by: java.lang.RuntimeException: Parcel android.os.Parcel@b389dec8: Unmarshalling unknown type code 6619245 at offset 800
09-05 16:04:36.567: E/AndroidRuntime(1976): at android.os.Parcel.readValue(Parcel.java:2032)
09-05 16:04:36.567: E/AndroidRuntime(1976): at android.os.Parcel.readSparseArrayInternal(Parcel.java:2255)
09-05 16:04:36.567: E/AndroidRuntime(1976): at android.os.Parcel.readSparseArray(Parcel.java:1687)
09-05 16:04:36.567: E/AndroidRuntime(1976): at android.os.Parcel.readValue(Parcel.java:2022)
09-05 16:04:36.567: E/AndroidRuntime(1976): at android.os.Parcel.readMapInternal(Parcel.java:2226)
09-05 16:04:36.567: E/AndroidRuntime(1976): at android.os.Bundle.unparcel(Bundle.java:223)
09-05 16:04:36.567: E/AndroidRuntime(1976): at android.os.Bundle.getSparseParcelableArray(Bundle.java:1232)
09-05 16:04:36.567: E/AndroidRuntime(1976): at com.android.internal.policy.impl.PhoneWindow.restoreHierarchyState(PhoneWindow.java:1605)
09-05 16:04:36.567: E/AndroidRuntime(1976): at android.app.Activity.onRestoreInstanceState(Activity.java:928)
09-05 16:04:36.567: E/AndroidRuntime(1976): at android.app.Activity.performRestoreInstanceState(Activity.java:900)
09-05 16:04:36.567: E/AndroidRuntime(1976): at android.app.Instrumentation.callActivityOnRestoreInstanceState(Instrumentation.java:1130)
09-05 16:04:36.567: E/AndroidRuntime(1976): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2037)
09-05 16:04:36.567: E/AndroidRuntime(1976): ... 11 more
我知道当我们尝试以错误的顺序读取包裹时会发生这种情况,但如果是这种情况,Exception
当我们旋转设备时不应该发生这种情况吗?我相信它遵循完全相同的过程,但我不能 100% 确定今天之后。
顺便说一句,这只发生Activity
在我添加了 custom的地方View
。实现与FragmentTabHost
支持库中的实现相同(实际上,我复制并修改了该源代码),除了我做了一些更改。但是这些更改不会影响之前的FragmentTabHost
State 代码。以下是相关的代码片段 -
static class SavedState extends BaseSavedState {
String curTab;
SavedState(final Parcelable superState) {
super(superState);
}
private SavedState(final Parcel in) {
super(in);
curTab = in.readString();
}
@Override
public void writeToParcel(final Parcel out, final int flags) {
super.writeToParcel(out, flags);
out.writeString(curTab);
}
@Override
public String toString() {
return "SavedStateFragmentTabHost.SavedState{"
+ Integer.toHexString(System.identityHashCode(this))
+ " curTab=" + curTab + "}";
}
public static final Parcelable.Creator<SavedState> SAVED_STATE_CREATOR = new Parcelable.Creator<SavedState>() {
@Override
public SavedState createFromParcel(final Parcel in) {
return new SavedState(
in);
}
@Override
public SavedState[] newArray(final int size) {
return new SavedState[size];
}
};
}
@Override
protected Parcelable onSaveInstanceState() {
final Parcelable superState = super.onSaveInstanceState();
final SavedState ss = new SavedState(superState);
ss.curTab = getCurrentTabTag();
return ss;
}
@Override
protected void onRestoreInstanceState(final Parcelable state) {
final SavedState ss = (SavedState) state;
super.onRestoreInstanceState(ss.getSuperState());
setCurrentTabByTag(ss.curTab);
}
如果我在我的 custom 中注释掉onSaveInstanceState()
and方法,则不会发生。以前有没有其他人遇到过这个问题?你能设法解决它吗?onRestoreInstanceState()
FragmentTabHost
Exception