Android中用onSaveInstanceState保存Fragment状态的方法
在Fragment里面,利用onSaveInstanceState保存数据,并可在onActivityCreated里面恢复数据。
public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); ... if (savedInstanceState != null) { // Restore the fragment's state here } } public void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); // Save the fragment's state here }
在Activity里面,利用onSaveInstanceState保存Fragment实例的数据,并可在onCreate里面恢复数据:
private Fragment myFragment; public void onCreate(Bundle savedInstanceState) { ... if (savedInstanceState == null) { // Instantiate fragment } else { // Restore the fragment's instance myFragment = getSupportFragmentManager().getFragment( savedInstanceState, "fragment"); } ... } @Override protected void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); //Save the fragment's instance // fragment instance may be null if (myFragment != null) { getSupportFragmentManager().putFragment(outState, "fragment", myFragment); } }
实例
首先看一个步骤,如果是人为关闭Activity或者Fragment, 比如点击了后退键,主动关闭当前页面,就会走 onPause() --> onStop() --> onDestroy() 而onSaveInstanceState() 并不会被调用. onSaveInstanceState() 只有在系统即将要自动清理销毁Activity或Fragment前才会调用, 比如
1, 由于重力感应 手机从竖屏变为横屏,
2, 手机点击Home键和长按Home键
3, 点击电源键锁屏时
4, 从当前Activity跳到另一个Activity
5, 应用内存不足即将自动销毁时等情况
综合上面的情况,可以看出, onSaveInstanceState() 这个方法适合临时保存一些非永久性的数据.如果要持久化保存数据,就要将操作放在onStop(), onDestroy()这些方法中去.onSaveInstanceState() 适合的是当前Activity或者Fragment一旦被系统自己销毁,应用还能在这之前保存一些必要的数据, 并且用户的操作很快又回到当前这个页面,这个时候数据并没有丢失,还能最大程度的恢复之前状态.这就是这个方法最大的意义.下面上例子做个简单示例:
在Fragment中:
//系统自动销毁Fragment前保存必要的数据 @Override public void onSaveInstanceState(Bundle outState){ <span style="white-space:pre"> </span>super.onSaveInstanceState(outState); <span style="white-space:pre"> </span>String content = etCon.getText().toString(); <span style="white-space:pre"> </span>outState.putString("inputCon", content); } //恢复数据 @Override public void onViewStateRestored(Bundle savedInstanceState){ <span style="white-space:pre"> </span>super.onViewStateRestored(savedInstanceState); <span style="white-space:pre"> </span>//恢复之前输入框的内容 <span style="white-space:pre"> </span>if(savedInstanceState != null){ <span style="white-space:pre"> </span>etCon.setText(savedInstanceState.getString("inputCon", "")); } }
在Activity中
//系统自动销毁Activity前保存必要的数据 @Override public void onSaveInstanceState(Bundle outState){ <span style="white-space:pre"> </span>super.onSaveInstanceState(outState); String content = etCon.getText().toString(); outState.putString("inputCon", content); } //Activity中可在onRestoreInstanceState()方法恢复数据,也可以在onCreate()中恢复,因为Activity一旦被销毁就会重走一次onCreate()开始的正常生命周期 @Override public void onRestoreInstanceState(Bundle savedInstanceState){ super.onRestoreInstanceState(savedInstanceState); //恢复之前输入框的内容 if(savedInstanceState != null){ etCon.setText(savedInstanceState.getString("inputCon", "")); } } @Override public void onDestroy(){ super.onDestroy(); //Activity被销毁前可进行最后一次数据的保存,过了这村就没这店了. }
这里有个重要的点,上面的上面所提及的onSaveInstanceState()方法只有在Activity或Fragment确定要被系统自动清除时,才会被调用,如果不是长期后台应用,页面没有被系统kill掉,对应的Activity或Fragment也就不会调用onSaveInstanceState()这些方法.