eg:: Fragment-A
)加载到容器中
onSaveInstanceState
事件我将一些数据存储到包我使用代码
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putInt("yourSpinner", spnSearchByCity.getSelectedItemPosition());
}
replace
container
点击按钮Fragment-B
在Fragment-A
我使用代码
fragment = FrgMdMap.newInstance(messengerObj);
if (fragment != null) {
getFragmentManager().beginTransaction().replace(R.id.content_frame, fragment).addToBackStack(null).commit();
}
我已成功加载Fragment-B
现在关于Fragment-B的方向更改onSaveInstanceState
Fragment-A
id
答案 0 :(得分:2)
How is this taking place ?
实际上Fragment-A
仍然存在且位于您Fragment-B
下面,因为您更换了片段后添加了addToBackStack(null)
。
How can i make sure this wont happen ?
您可以删除addToBackStack(null)
或计算onSaveInstanceState
方法中已堆叠片段的数量,当它为0时,您可以在onSaveInstanceState
修改强>
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
FragmentManager fm = getActivity().getSupportFragmentManager();
if(fm.getBackStackEntryCount() == 0)
outState.putInt("yourSpinner", spnSearchByCity.getSelectedItemPosition());
}