有很多种正常的状态导致Activity的销毁,例如用户点击返回键或直接调用finish()
方法。当Activity处于stopped状态且长时间不用时或前台Activity需要更多资源导致系统必须杀死后台进程回收内存时,系统会销毁你的Activity。当activity被销毁是因为用户点击返回键或调用finish()
方法时,这时该Activity的实例将永久的消失,因为这些行为表示该Activity不再被需要。然而,如果由于系统限制导致系统销毁了Activity,则尽管实际上Activity的实例是消失了,但系统会记录它的存在,这是如果用户回到它,系统会使用该Activity销毁时保存的状态数据重建一个新的Activity实例。这个系统保存的用来恢复到之前状态的数据叫做“实例状态”,它以键值对的形式保存在Bundle对象中。
用户每次旋转屏幕时,Activity都会被销毁后重建。这是因为当屏幕方向改变时,Activity可能需要加载不同的资源文件layout,所以前台的Activity会被销毁并重建。
默认情况下,系统会使用Bundle保存在Activity布局中定义的每一个View对象的信息(例如ListView的滚动位置)。所以,如果你的Activity被销毁后重建,布局状态的恢复不需要你编写任何的代码。然而,你的Activity可能有更多需要恢复的状态信息,例如保存用户状态的变量。
为了让系统能恢复你Activity中views的状态,每一个view需要有唯一的id,其通过android:id
属性提供。
为了保存关于activity状态的附加数据,你必须重写onSaveInstanceState()
回调方法。当用户离开该Activity时(不管是不是意外退出),该方法被回调,系统传递保存该Activity意外销毁时的视图状态的Bundle到该方法。如果系统稍后需要重建该Activity,它传递这个相同的Bundle对象到onCreate()
和onRestoreInstanceState()
方法。
注意使用Bundle保存状态并不总是有效的,这是因为Bundle中保存的数据必须是经过序列化的(实现了Parcelable或Serializable接口)且Bundle中只能保存1M以下的数据。
下图显示了Activity意外销毁时,状态的保存和恢复的回调过程:
public class StateSaveAndRestoreActivity extends AppCompatActivity {
private static final String TAG = StateSaveAndRestoreActivity.class.getSimpleName();
private static final String SXD = "sxd";
private static final String STATE_KEY = "state_key";
private EditText mEditText;//EditText中的值,系统默认就会恢复
private String mEditTextStr;//状态变量的值,需要自己保存和恢复
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.state_save_and_restore_activity);
if (savedInstanceState == null) {
Log.i(SXD, TAG + "--onCreate++savedInstanceState:" + savedInstanceState);
} else {
Log.i(SXD, TAG + "--onCreate++savedInstanceState++mEditTextStr:" + savedInstanceState.getString(STATE_KEY));
}
initView();
}
private void initView() {
mEditText = (EditText) this.findViewById(R.id.edit_text);
mEditText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
mEditTextStr = mEditText.getText().toString();
}
});
}
@Override
protected void onStart() {
super.onStart();
Log.i(SXD, TAG + "--onStart");
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
mEditTextStr = savedInstanceState.getString(STATE_KEY);
Log.i(SXD, TAG + "--onRestoreInstanceState++savedInstanceState++mEditTextStr:" + mEditTextStr);
}
@Override
protected void onResume() {
super.onResume();
Log.i(SXD, TAG + "--onResume");
}
@Override
protected void onPause() {
super.onPause();
Log.i(SXD, TAG + "--onPause");
}
@Override
protected void onStop() {
super.onStop();
Log.i(SXD, TAG + "--onStop");
}
@Override
protected void onDestroy() {
super.onDestroy();
Log.i(SXD, TAG + "--onDestroy");
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putString(STATE_KEY, mEditTextStr);
Log.i(SXD, TAG + "--onSaveInstanceState++outState++mEditTextStr:" + outState.getString(STATE_KEY));
}
}
以上程序输入“哈哈哈”后,执行屏幕翻转
执行完屏幕翻转后的Log输出:
因为onCreate()
方法不管是创建新的Activity还是重建老的Activity都会被调用,所以使用onCreate()
方法中的Bundle恢复状态时,必须判断Bundle是否为null,如果为null,则创建新的Activity,否则重建老的Activity。
如果使用onRestoreInstanceState()
方法恢复状态,则不再需要对其中的Bundle进行判null,因为仅当有状态需要被恢复时,该方法才会被回调,且该方法在onStart()
方法之后调用。
注意:
onSaveInstanceState()
方法超类的实现,因为默认实现能保存views的状态。onRestoreInstanceState()
方法超类的实现,因为默认实现能够恢复views的状态。 当系统因为配置变化关闭Activity时,该Activity中的fragments会被保持而不被销毁。
当Fragment的宿主Activity意外销毁时,系统会将Fragment的当时状态保存到Bundle对象中,以便当该Activity重建时,可以将Fragment恢复到之前的状态。
同Activity一样,默认情况下,系统会使用Bundle保存在Fragment布局中定义的每一个View对象的信息(例如ListView的滚动位置)。所以,如果你的Fragment被销毁后重建,布局状态的恢复不需要你编写任何的代码。然而,你的Fragment可能有更多需要恢复的状态信息,例如保存用户状态的变量。
为了让系统能恢复你Fragment中views的状态,每一个view需要有唯一的id,其通过android:id
属性提供。
为了保存关于Fragment状态的附加数据,必须重写onSaveInstanceState()
回调方法。当用户离开该Fragment的宿主Activity时(不管是不是意外退出),该方法被回调,系统传递保存该Fragment当前的视图状态的Bundle到该方法。如果系统稍后需要重建该Fragment的宿主Activity,它传递这个相同的Bundle对象到onCreate()
(当调用setRetainInstance(true)
后,在恢复状态时不再回调该方法)、onCreateView()
、onViewCreated()
、onActivityCreated()
和onViewStateRestored()
方法。
相对Activity,Fragment有一种很容易保存状态的方法,就是调用setRetainInstance(true)
方法,将该Fragment转变为“持久化Fragment”,这意味着只有该Fragment的GUI(从onViewCreated()
方法返回的View)会被销毁重建,而所有其他的引用变量仍然保持。这就不需要额外的将状态变量保存在Bundle中了,从而简化了很多操作。
注意在Fragment的宿主Activity重建时,使用FragmentManager取回该Fragment,而不要新建一个这样的Fragment。
public class FragmentStateDealActivity extends AppCompatActivity {
private static final String TAG = FragmentStateDealActivity.class.getSimpleName();
private static final String SXD = "sxd";
public static final String STR_STATE_KEY = "str_state_key";
private StateSaveAndRestoreFragment mStateSaveAndRestoreFragment;
private FragmentManager mFm;
private String mString;//状态变量的值,需要自己保存和恢复
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_state_deal_activity);
mFm = getSupportFragmentManager();
mStateSaveAndRestoreFragment = (StateSaveAndRestoreFragment) mFm.findFragmentByTag(StateSaveAndRestoreFragment.TAG);
Log.i(SXD, TAG + "--onCreate++mStateSaveAndRestoreFragment:" + mStateSaveAndRestoreFragment);
if (mStateSaveAndRestoreFragment == null) {
mStateSaveAndRestoreFragment = StateSaveAndRestoreFragment.newInstance();
mFm.beginTransaction().add(R.id.content_layout, mStateSaveAndRestoreFragment, StateSaveAndRestoreFragment.TAG).commit();
}
if (savedInstanceState == null) {
Log.i(SXD, TAG + "--onCreate++savedInstanceState:" + savedInstanceState);
} else {
Log.i(SXD, TAG + "--onCreate++savedInstanceState++mString:" + savedInstanceState.getString(STR_STATE_KEY));
}
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
Log.i(SXD, TAG + "--onRestoreInstanceState++onRestoreInstanceState++mString:" + savedInstanceState.getString(STR_STATE_KEY));
}
@Override
protected void onResume() {
super.onResume();
mString = "Fragment宿主中的变量";
Log.i(SXD, TAG + "--onResume++mString:" + mString);
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putString(STR_STATE_KEY, mString);
Log.i(SXD, TAG + "--onSaveInstanceState++outState++mString:" + outState.getString(STR_STATE_KEY));
}
@Override
protected void onDestroy() {
super.onDestroy();
Log.i(SXD, TAG + "--onDestroy");
}
}
从示例代码中可以看到,在onCreate()
中,先通过FragmentManager取回StateSaveAndRestoreFragment,如果之前向FragmentManager中添加过StateSaveAndRestoreFragment,则取出之前添加的StateSaveAndRestoreFragment,以便能够恢复到销毁前的状态,如果之前没有添加过,则新建StateSaveAndRestoreFragment对象并将其添加进FragmentManager中。
setRetainInstance(true)
的示例/** * 通过setRetainInstance(true)将该Fragment设置为“保持Fragment“ */
public class StateSaveAndRestoreFragment extends Fragment {
public static final String TAG = StateSaveAndRestoreFragment.class.getSimpleName();
private static final String SXD = "sxd";
private EditText mEditText;//EditText中的值,系统默认就会恢复
private String mEditTextStr;//状态变量的值,当通过setRetainInstance(true)将该Fragment设置为“保持Fragment“,则该变量在宿主Activity销毁重建时,会一直保持
public static StateSaveAndRestoreFragment newInstance() {
StateSaveAndRestoreFragment stateSaveAndRestoreFragment = new StateSaveAndRestoreFragment();
return stateSaveAndRestoreFragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRetainInstance(true);
Log.i(SXD, TAG + "--onCreate");
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.state_save_and_restore_fragment, container, false);
Log.i(SXD, TAG + "--onCreateView++mEditTextStr:" + mEditTextStr);
initView(rootView);
return rootView;
}
private void initView(View rootView) {
mEditText = (EditText) rootView.findViewById(R.id.edit_text);
mEditText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
mEditTextStr = mEditText.getText().toString();
}
});
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
Log.i(SXD, TAG + "--onViewCreated");
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
Log.i(SXD, TAG + "--onActivityCreated");
}
@Override
public void onViewStateRestored(@Nullable Bundle savedInstanceState) {
super.onViewStateRestored(savedInstanceState);
Log.i(SXD, TAG + "--onViewStateRestored");
}
@Override
public void onStart() {
super.onStart();
Log.i(SXD, TAG + "--onStart");
}
@Override
public void onResume() {
super.onResume();
Log.i(SXD, TAG + "--onResume");
}
@Override
public void onHiddenChanged(boolean hidden) {
super.onHiddenChanged(hidden);
Log.i(SXD, TAG + "--onHiddenChanged++hidden:" + hidden);
}
@Override
public void onPause() {
super.onPause();
Log.i(SXD, TAG + "--onPause");
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
Log.i(SXD, TAG + "--onSaveInstanceState++mEditTextStr:" + mEditTextStr);
}
@Override
public void onStop() {
super.onStop();
Log.i(SXD, TAG + "--onStop");
}
@Override
public void onDestroyView() {
super.onDestroyView();
Log.i(SXD, TAG + "--onDestroyView");
}
@Override
public void onDestroy() {
super.onDestroy();
Log.i(SXD, TAG + "--onDestroy");
}
}
同Activity的状态保存与恢复示例一样的输入“哈哈哈”,并旋转屏幕后的Log输出:
从输出结果可以得到:
setRetainInstance(true)
将该Fragment设置为“保持Fragment“后,则其中的状态变量在宿主Activity销毁重建时,会一直保持,不会销毁。setRetainInstance(true)
将该Fragment设置为“保持Fragment“后,当宿主Activity销毁重建时,不会回调该Fragment的onDestroy()
和onCreate()
方法,即这时只销毁重建View,并不会销毁状态变量。setRetainInstance(true)
的示例public class StateSaveAndRestoreFragment extends Fragment {
public static final String TAG = StateSaveAndRestoreFragment.class.getSimpleName();
private static final String SXD = "sxd";
private static final String STATE_KEY = "state_key";
private EditText mEditText;//EditText中的值,系统默认就会恢复
private String mEditTextStr;//状态变量的值,需要自己保存和恢复
public static StateSaveAndRestoreFragment newInstance() {
StateSaveAndRestoreFragment stateSaveAndRestoreFragment = new StateSaveAndRestoreFragment();
return stateSaveAndRestoreFragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.i(SXD, TAG + "--onCreate++savedInstanceState++mEditTextStr:" + mEditTextStr);
if (savedInstanceState == null) {
Log.i(SXD, TAG + "--onCreate++savedInstanceState:" + savedInstanceState);
} else {
mEditTextStr = savedInstanceState.getString(STATE_KEY);
Log.i(SXD, TAG + "--onCreate++savedInstanceState++mEditTextStr:" + mEditTextStr);
Log.i(SXD, TAG + "--onCreate++savedInstanceState++mString:" + savedInstanceState.getString(FragmentStateDealActivity.STR_STATE_KEY));
}
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.state_save_and_restore_fragment, container, false);
if (savedInstanceState == null) {
Log.i(SXD, TAG + "--onCreateView++savedInstanceState:" + savedInstanceState);
} else {
Log.i(SXD, TAG + "--onCreateView++savedInstanceState++mEditTextStr:" + savedInstanceState.getString(STATE_KEY));
Log.i(SXD, TAG + "--onCreateView++savedInstanceState++mString:" + savedInstanceState.getString(FragmentStateDealActivity.STR_STATE_KEY));
}
initView(rootView);
return rootView;
}
private void initView(View rootView) {
mEditText = (EditText) rootView.findViewById(R.id.edit_text);
mEditText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
mEditTextStr = mEditText.getText().toString();
}
});
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
if (savedInstanceState == null) {
Log.i(SXD, TAG + "--onViewCreated++savedInstanceState:" + savedInstanceState);
} else {
Log.i(SXD, TAG + "--onViewCreated++savedInstanceState++mEditTextStr:" + savedInstanceState.getString(STATE_KEY));
Log.i(SXD, TAG + "--onViewCreated++savedInstanceState++mString:" + savedInstanceState.getString(FragmentStateDealActivity.STR_STATE_KEY));
}
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
if (savedInstanceState == null) {
Log.i(SXD, TAG + "--onActivityCreated++savedInstanceState:" + savedInstanceState);
} else {
Log.i(SXD, TAG + "--onActivityCreated++savedInstanceState++mEditTextStr:" + savedInstanceState.getString(STATE_KEY));
Log.i(SXD, TAG + "--onActivityCreated++savedInstanceState++mString:" + savedInstanceState.getString(FragmentStateDealActivity.STR_STATE_KEY));
}
}
@Override
public void onViewStateRestored(@Nullable Bundle savedInstanceState) {
super.onViewStateRestored(savedInstanceState);
if (savedInstanceState == null) {
Log.i(SXD, TAG + "--onViewStateRestored++savedInstanceState:" + savedInstanceState);
} else {
Log.i(SXD, TAG + "--onViewStateRestored++savedInstanceState++mEditTextStr:" + savedInstanceState.getString(STATE_KEY));
Log.i(SXD, TAG + "--onViewStateRestored++savedInstanceState++mString:" + savedInstanceState.getString(FragmentStateDealActivity.STR_STATE_KEY));
}
}
@Override
public void onStart() {
super.onStart();
Log.i(SXD, TAG + "--onStart");
}
@Override
public void onResume() {
super.onResume();
Log.i(SXD, TAG + "--onResume");
}
@Override
public void onHiddenChanged(boolean hidden) {
super.onHiddenChanged(hidden);
Log.i(SXD, TAG + "--onHiddenChanged++hidden:" + hidden);
}
@Override
public void onPause() {
super.onPause();
Log.i(SXD, TAG + "--onPause");
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putString(STATE_KEY, mEditTextStr);
Log.i(SXD, TAG + "--onSaveInstanceState++outState++mEditTextStr:" + outState.getString(STATE_KEY));
}
@Override
public void onStop() {
super.onStop();
Log.i(SXD, TAG + "--onStop");
}
@Override
public void onDestroyView() {
super.onDestroyView();
Log.i(SXD, TAG + "--onDestroyView");
}
@Override
public void onDestroy() {
super.onDestroy();
Log.i(SXD, TAG + "--onDestroy");
}
}
同Activity的状态保存与恢复示例一样的输入“哈哈哈”,并旋转屏幕后的Log输出:
从输出结果可以看出以下几点:
onCreate()
、onCreateView()
、onViewCreated()
、onActivityCreated()
和onViewStateRestored()
方法中的Bundle和Framgent调用onSaveInstanceState()
方法保存状态的中的Bundle是一样的。onRestoreInstanceState()
方法只在状态需要被恢复时调用,Framgent的onViewStateRestored()
方法有没有状态需要被恢复时都会被调用。setRetainInstance(true)
方法将Fragment设置为“保持的Fragment”,则当Fragment的宿主Activity销毁时,该Fragment会将状态变量一同销毁,所以示例中mEditTextStr的值在屏幕旋转后变为null。onCreate()
、onCreateView()
、onViewCreated()
、onActivityCreated()
和onViewStateRestored()
这些回调方法中的Bundle对象和其宿主Activity没有任何关系,不能从中获取到其宿主Activity保存到Bundle对象中的数据。 View没有像Fragment一样让其变成“保持”状态的方法,所以当包含状态变量的自定义View需要销毁重建时,需要在View销毁时回调protected Parcelable onSaveInstanceState()
方法中,将需要保存的状态变量保存在实现了Parcelable接口的数据对象中,并在View重建调用protected void onRestoreInstanceState(Parcelable state)
时,从state(其为onSaveInstanceState()
方法返回的数据对象)中将这些状态变量恢复。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent">
<com.example.sunxiaodong.statesaveandrestore.StateSaveAndRestoreLinearLayout android:id="@+id/linear_layout" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true">
<EditText android:id="@+id/edit_text" android:layout_width="80dp" android:layout_height="40dp" android:layout_centerInParent="true" />
</com.example.sunxiaodong.statesaveandrestore.StateSaveAndRestoreLinearLayout>
</RelativeLayout>
public class ViewStateDealActivity extends AppCompatActivity {
private static final String TAG = ViewStateDealActivity.class.getSimpleName();
private static final String SXD = "sxd";
private StateSaveAndRestoreLinearLayout mLinearLayout;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.view_state_deal_activity);
initView();
}
private void initView() {
mLinearLayout = (StateSaveAndRestoreLinearLayout) this.findViewById(R.id.linear_layout);
}
@Override
protected void onDestroy() {
super.onDestroy();
}
}
public class StateSaveAndRestoreLinearLayout extends LinearLayout {
private static final String TAG = StateSaveAndRestoreLinearLayout.class.getSimpleName();
private static final String SXD = "sxd";
private Integer mStateVar;//状态变量,当该View销毁时,其会被销毁
public StateSaveAndRestoreLinearLayout(Context context) {
super(context);
Log.i(SXD, TAG + "--StateSaveAndRestoreLinearLayout++1");
}
public StateSaveAndRestoreLinearLayout(Context context, AttributeSet attrs) {
super(context, attrs);
Log.i(SXD, TAG + "--StateSaveAndRestoreLinearLayout++beforeRandom++mStateVar:" + mStateVar);
mStateVar = new Random().nextInt(10);
Log.i(SXD, TAG + "--StateSaveAndRestoreLinearLayout++afterRandom++mStateVar:" + mStateVar);
}
public StateSaveAndRestoreLinearLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
Log.i(SXD, TAG + "--StateSaveAndRestoreLinearLayout++3");
}
@Override
protected void onRestoreInstanceState(Parcelable state) {
SaveState savedState = (SaveState) state;
super.onRestoreInstanceState(savedState.getParcelable());
mStateVar = savedState.getSavedState();
Log.i(SXD, TAG + "--onRestoreInstanceState++mStateVar:" + mStateVar);
}
@Override
protected Parcelable onSaveInstanceState() {
Log.i(SXD, TAG + "--onSaveInstanceState++mStateVar:" + mStateVar);
SaveState state = new SaveState(super.onSaveInstanceState());
state.setSavedState(mStateVar);
return state;
}
/** * 保存View状态数据体 */
static class SaveState implements Parcelable {
private Integer mSavedState;//保存View中的状态变量
private Parcelable mParcelable;//保存View的默认状态
SaveState(Parcelable parcelable) {
mParcelable = parcelable;
}
protected SaveState(Parcel in) {
mSavedState = in.readInt();
}
public static final Creator<SaveState> CREATOR = new Creator<SaveState>() {
@Override
public SaveState createFromParcel(Parcel in) {
return new SaveState(in);
}
@Override
public SaveState[] newArray(int size) {
return new SaveState[size];
}
};
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(mSavedState);
}
public Integer getSavedState() {
return mSavedState;
}
public void setSavedState(Integer mSavedState) {
this.mSavedState = mSavedState;
}
public Parcelable getParcelable() {
return mParcelable;
}
public void setParcelable(Parcelable mParcelable) {
this.mParcelable = mParcelable;
}
}
}
将屏幕旋转后的Log输出:
从输出可以得到:
protected void onRestoreInstanceState(Parcelable state)
只在View重建时回调。protected Parcelable onSaveInstanceState()
和protected void onRestoreInstanceState(Parcelable state)
中使用新的数据对象保存和恢复状态时,一定要将默认的View状态保存和恢复,即在onSaveInstanceState()
方法中,保存super.onSaveInstanceState()
,并在onRestoreInstanceState(Parcelable state)
中使用super.onRestoreInstanceState(savedState.getParcelable())
对之前保存的状态进行恢复。源码地址