在应用程序中,只要片段失去焦点(即在其上打开另一个活动/片段),就会调用onSaveInstanceState().
同样在developers guide中提到.
我试图使用这个approch来恢复我的片段的状态.
我的目的是在片段恢复时在onActivityCreated()中调用此包.
虽然在片段失去焦点之前调用onSaveInstanceState.
但是,当调用onActivityCreated()时,它会将Bundle savedInstanceState作为null返回.
如何从捆绑包中获取数据.
码:
@Override public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); Mylistadapter adapter = new Mylistadapter(getActivity(),R.layout.my_row,titles,icons,this); setlistadapter(adapter); if (savedInstanceState != null) { // Never goes inside this condiiton. // Restore last state for checked position. mCurCheckPosition = savedInstanceState.getInt("curChoice",0); } } @Override public void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); outState.putInt("curChoice",mCurCheckPosition); }
解决方法
然后它意味着您的活动和片段刚刚暂停,并且不必保存其实例状态(不一定要调用onSaveInstanceState()).
如果要确保调用onSaveInstanceState(),请在设备的开发人员设置中选择“不要保留活动”.然后,每次活动暂停时,系统会将其强制执行,从而强制调用onSaveInstanceState()(当然,只需按下当然)