这是后续操作:Android local variable get's lost when using camera intent
正确的方法是处理onSaveInstanceState
和onRestoreInstanceState
,如下所示:
https://android.googlesource.com/platform/frameworks/base/+/master/core/java/android/widget/CompoundButton.java
这是我的代码:
static class SavedState extends BaseSavedState
{
private String requestedFileName;
private UUID[] mImages = new UUID[4];
SavedState(Parcelable superState)
{
super(superState);
}
private SavedState(Parcel in)
{
super(in);
}
@Override
public void writeToParcel(Parcel out, int flags)
{
super.writeToParcel(out, flags);
}
//required field that makes Parcelables from a Parcel
public static final Parcelable.Creator<SavedState> CREATOR =
new Parcelable.Creator<SavedState>()
{
public SavedState createFromParcel(Parcel in)
{
return new SavedState(in);
}
public SavedState[] newArray(int size)
{
return new SavedState[size];
}
};
}
@Override
public Parcelable onSaveInstanceState()
{
//begin boilerplate code that allows parent classes to save state
Parcelable superState = super.onSaveInstanceState();
SavedState ss = new SavedState(superState);
//end
ss.requestedFileName = this.requestedFileName;
ss.mImages = this.mImages;
return ss;
}
@Override
public void onRestoreInstanceState(Parcelable state)
{
//begin boilerplate code so parent classes can restore state
if(!(state instanceof SavedState))
{
super.onRestoreInstanceState(state);
return;
}
SavedState ss = (SavedState)state;
super.onRestoreInstanceState(ss.getSuperState());
//end
this.requestedFileName = ss.requestedFileName;
this.mImages = ss.mImages;
RestoreImageState();
}
现在我的问题。该代码似乎正常工作,并且可以毫无问题地处理所有状态更改。但是,如果您查看SavedState.writeToParcel和SavedState.SavedState,您会注意到我没有在其中存储变量。我一定要吗?为什么?问题是我了解如何wrteToParcel和我的数据类型匹配。但是,对于复杂类型,从包裹中读取内容并不清楚。在我的测试中,它没有被调用。
编辑:
这对于保存/退回订单是否正确?
private SavedState(Parcel in)
{
super(in);
this.mName = in.readString();
this.mIndex = in.readInt();
this.mApplicableDamages = (String[])in.readValue(null);
this.mSelectedDamages = (boolean[])in.readValue(null);
}
@Override
public void writeToParcel(Parcel out, int flags)
{
super.writeToParcel(out, flags);
out.writeString(this.mName);
out.writeInt(this.mIndex);
out.writeArray(this.mApplicableDamages);
out.writeBooleanArray(this.mSelectedDamages);
}
最佳答案
如果它们是视图状态的一部分,并且您不想在创建父活动时丢失它们,则必须将这些变量保存在onSaveInstanceState()
中,并稍后在onRestoreInstanceState()
中还原它们。
这些变量可能会丢失,因为发生配置更改时,父活动被破坏,并且新的活动对象被重新创建。该新对象将收到以前保存的状态。而且,如果您不向状态添加一些变量,则不会恢复。
关于将复杂类型写入Parcel
和从中读取Parcelable
,您可以为复杂类型的某些部分实现UUID[]
。或者,您可以仅将复杂类型分解为原始(可打包)字段,然后将这些字段一一存储。在您的情况下,Parcel
可以作为Serializable
存储在UUID[]
中,或者可以将ParcelUuid[]
转换为SavedState
并将此数组存储为可拆分数组。
还有关于SavedState
实现的几句话。如果您不将Parcel
中的字段添加到Parcel
中,则不会保存这些字段。因此,您必须将SavedState.writeToParcel()
方法写入SavedState(Parcel)
。而且,您还需要在构造函数中将它们读回。
static class SavedState extends BaseSavedState
{
private String requestedFileName;
private UUID[] mImages = new UUID[4];
SavedState(Parcelable superState)
{
super(superState);
}
private SavedState(Parcel in)
{
super(in);
requestedFileName = in.readString();
mImages = (UUID[])in.readSerializable();
}
@Override
public void writeToParcel(Parcel out, int flags)
{
super.writeToParcel(out, flags);
out.writeString(requestedFileName);
out.writeSerializable(mImages);
}
//required field that makes Parcelables from a Parcel
public static final Parcelable.Creator<SavedState> CREATOR =
new Parcelable.Creator<SavedState>()
{
public SavedState createFromParcel(Parcel in)
{
return new SavedState(in);
}
public SavedState[] newArray(int size)
{
return new SavedState[size];
}
};
}