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()这些方法.