当我的应用进入后台时,我看到onSavedInstanceState
被调用,我尝试使用Parcelable
保存对象的实例。但是,当我的应用返回到前台时,尽管我将数据保存在onResume
中,但仅调用onCreate
,从不调用onRestoreInstanceState
或onSavedInstanceState
。当我继续使用该应用程序时,数据确实丢失了并且崩溃了。因此,我需要确保状态已重新创建。
根据文档:
“仅当存在要还原的保存状态时,系统才会调用onRestoreInstanceState(),因此您无需检查Bundle是否为空。”
我看到打印输出onSavedInstanceState
调用了-----Saving Session-----
。
该应用程序崩溃是因为进入pause
模式时数组中某些位置计数器丢失了。因此,这并不是真正的问题,只是表明我需要恢复Session
实例的状态。
编辑:由于我的一个愚蠢错误,该应用程序崩溃了。数据实际上不需要保存。但是,尽管文档说明如此,但onSaveInstanceState
被调用,但从未被onRestoreInstanceState
调用。我还是不明白。
生命周期如下:
OnPause
OnSaveInstanceState
OnStop
OnStart
OnResume
这是我保存对象的方式:
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putParcelable(SESSION_KEY, mSession);
System.out.println(" ----- Saving Session --------");
}
还原:
@Override
protected void onRestoreInstanceState(Bundle bundle) {
System.out.println("OnRestoreInstanceState");
super.onRestoreInstanceState(bundle);
String tmp = bundle.getString("TEST_KEY");
System.out.println(tmp);
}
在Parcelable
中实施Session
:
protected Session(Parcel in) {
mFinalists = in.readArrayList(Finalist.class.getClassLoader());
shootingOrder = in.readParcelable(ShootingOrder.class.getClassLoader());
nbrshots = in.readInt();
nextSession = in.readArrayList(Integer.class.getClassLoader());
remaining = in.readArrayList(Integer.class.getClassLoader());
eliminated = in.readArrayList(Integer.class.getClassLoader());
circleIndex = in.readArrayList(Integer.class.getClassLoader());
mOldStations = in.readArrayList(OldStations.class.getClassLoader());
}
public static final Creator<Session> CREATOR = new Creator<Session>() {
@Override
public Session createFromParcel(Parcel in) {
System.out.println("Restoring Session");
return new Session(in);
}
@Override
public Session[] newArray(int size) {
return new Session[size];
}
};
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeList(mFinalists);
dest.writeParcelable(shootingOrder, flags);
dest.writeInt(nbrshots);
dest.writeList(nextSession);
dest.writeList(remaining);
dest.writeList(eliminated);
dest.writeList(circleIndex);
dest.writeList(mOldStations);
dest.writeList(mCurrentStation);
dest.writeList(mCurrentColors);
boolean tmp[] = {isShootOff, isInitilized};
dest.writeBooleanArray(tmp);
dest.writeInt(sOffShots);
}
我还将Finalist
,ShootingOrder
和OldStation
实现为Parcelable
:
protected Finalist(Parcel in) {
int tmp[] = new int[100];
in.readIntArray(tmp);
mName = in.readString();
mPosition = in.readInt();
int resultSize = in.readInt();
result = new ArrayList<>();
for (int i = 0; i < resultSize; i++){
result.add(tmp[i]);
}
System.out.println("Restoring Finalist");
}
public static final Creator<Finalist> CREATOR = new Creator<Finalist>() {
@Override
public Finalist createFromParcel(Parcel in) {
return new Finalist(in);
}
@Override
public Finalist[] newArray(int size) {
return new Finalist[size];
}
};
private int[] getResultArray(){
int tmp[] = new int[result.size()];
for (int i = 0; i < result.size(); i++){
tmp[i] = result.get(i);
}
return tmp;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
int tmp[] = getResultArray();
dest.writeIntArray(tmp);
dest.writeString(mName);
dest.writeInt(mPosition);
dest.writeInt(result.size());
}