我的 Android 应用程序有一个ActionBar
变化,它Fragment
占据了一定的FrameLayout
. 我正在尝试onSaveInstanceState
在选项卡更改时保存 Fragment 的状态,以便可以在onCreateView
.
问题是,onSaveInstanceState
永远不会被调用。' Fragment
sonDestroyView
和onCreateView
方法被调用,但Bundle
提供的 toonCreateView
保持为空。
有人可以向我解释何时onSaveInstanceState
实际调用它,我如何确保在切换选项卡时调用它,或者在Fragment
分离和重新连接时保存和恢复状态的最佳实践?
分段:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.event_log, container, false);
// Retrieve saved state
if (savedInstanceState != null){
System.out.println("log retrieved");
} else {
System.out.println("log null");
}
return view;
}
@Override
public void onSaveInstanceState(Bundle outState) {
System.out.println("log saved");
super.onSaveInstanceState(outState);
// more code
}
活动:
/**
* Detach the current Fragment, because another one is being attached.
*/
@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
if (tab.getText().equals(getString(R.string.tab_events))){
if (frEventLog != null) {
ft.detach(frEventLog);
}
}