通过Activity的onRetainNonConfigurationInstance()方法来保存一些状态信息。现在有了Fragment之后,可以更加方便的保存状态。
通过setRetainInstance()方法来确定是否让fragment保存状态。注意设置保存状态之后fragment的生命周期会有些变化。
public void setRetainInstance (boolean retain)
Since: API Level 11
Control whether a fragment instance is retained across Activity re-creation (such as froma configuration change). This can only be used with fragments not in the back stack. Ifset, the fragment lifecycle will be slightly different when an activity is recreated:
onDestroy() will not be called (but onDetach() still will be, because the fragment isbeing detached from its current activity).
onCreate(Bundle) will not be called since the fragment is not being re-created.
onAttach(Activity) and onActivityCreated(Bundle) will still be called.