Android Activity Fragment 状态保存
首先查阅这几篇文章,了解acticity和fragment的保存过程。
[译]Android Activity 和 Fragment 状态保存与恢复的最佳实践
Android Fragment+ViewPager 组合,一些你不可不知的注意事项
Android Fragment使用(三) Activity, Fragment, WebView的状态保存和恢复
附带activity和fragment的生命周期图,看后应该比较容易理解。
看完后基本了解原理了,这里要将要点总结一下:
1.Back stack中的fragment,只是View的销毁和重建, 其本身并没有被重建,并且成员变量也没有被清除,所以这种情况不用做任何保存操作系统自动保存,当然listview和recycleview的数据需重新设置回去,setAdapter,也是下面的第3点
2.fragment + viewpage 时注意使用FragmentPagerAdapter 和 FragmentStatePagerAdapter 这个两个函数的不同
3.在使用fragment+recycleview或activity+recycleview时如何恢复原来状态和数据,其实也不是那么复杂,recycleview的位置系统将自动保存,你只需重新设置adapter的数据即可,注意:如果是fragment一定要在onCreateView()函数恢复,而且要在view组件完成Listener后恢复;如果是activiy则在onRestoreInstanceState(),同样要在view组件完成Listener后恢复;
recycleview能自动保存状态是因为实现了以下方法(系统自带),第三方view必须实现以下方法才能让系统正确保存状态
<pre>
public class CustomView extends View {
...
@Override
public Parcelable onSaveInstanceState() {
Bundle bundle = new Bundle();
// Save current View's state here
return bundle;
}
@Override
public void onRestoreInstanceState(Parcelable state) {
super.onRestoreInstanceState(state);
// Restore View's state here
}
...
}
</pre>
Activity恢复状态情况:
<pre>
public class MainActivity extends AppCompatActivity {
// These variable are destroyed along with Activity
private int someVarA;
private String someVarB;
...
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putInt("someVarA", someVarA);
outState.putString("someVarB", someVarB);
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
someVarA = savedInstanceState.getInt("someVarA");
someVarB = savedInstanceState.getString("someVarB");
}
...
}
</pre>
Fragment恢复状态情况:
<pre>
public class MainFragment extends Fragment {
// These variable are destroyed along with Activity
private int someVarA;
private String someVarB;
...
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putInt("someVarA", someVarA);
outState.putString("someVarB", someVarB);
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
someVarA = savedInstanceState.getInt("someVarA");
someVarB = savedInstanceState.getString("someVarB");
}
...
}
</pre>
保存服务器加载的数据:
<pre>
public class MainFragment extends Fragment {
...
private String dataGotFromServer;
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putString("dataGotFromServer", dataGotFromServer);
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
dataGotFromServer = savedInstanceState.getString("dataGotFromServer");
}
...
}
</pre>
3.在activity和fragment的生命周期函数中的onSaveInstanceState()不是每次新建或重建都会运行,只有在系统将它销毁或关闭屏幕时才会运行,在activity中的onRestoreInstanceState()只有重建才运行
4.组件必须设置id属性,如EditText 或者 TextView,只要设置了 android:freezeText="true",其他组建有id就行
所以请不要在Fragment或activity的 onSaveInstanceState 方法中保存 View 状态。