previous片段的onSaveInstanceState的上称为定向电流片段 [英] onSaveInstanceState of previous fragment is called on-orientation of current fragment
问题描述
我在做什么:
- 我打开一个片段(
例如::片段-A
)的容器,并在的onSaveInstanceState
事件我正在存储一些数据包
我用的是code 的
@覆盖
公共无效的onSaveInstanceState(捆绑outState){
super.onSaveInstanceState(outState); outState.putInt(yourSpinner,spnSearchByCity.getSelectedItemPosition()); }
- 现在,我
替换
的容器
与片段-B
按钮的点击
在片段-A
我用的是code 的
片段= FrgMdMap.newInstance(messengerObj); 如果(片段!= NULL){
。getFragmentManager()调用BeginTransaction()取代(R.id.content_frame,片段).addToBackStack(空).commit();
}
-
我能够成功加载
片段-B
-
现在的片段-B的方向改变的
的onSaveInstanceState
的片段-A
ID射击
问题:
- 这是怎么了结的地方?
- 如何确保这不会发生?
这是怎么了结的地方?
实际上片段-A
仍然活着,以及下方的片段-B
这是因为 addToBackStack(空)
补充,当你更换的片段。
如何确保这不会发生?
您可以删除 addToBackStack(空)
或计数在的onSaveInstanceState
法层叠片段的数量和时它是0,那么你可以运行里面的code你的的onSaveInstanceState
编辑:
公共无效的onSaveInstanceState(捆绑outState){
super.onSaveInstanceState(outState);
FragmentManager FM = getActivity()getSupportFragmentManager()。
如果(fm.getBackStackEntryCount()== 0)
outState.putInt(yourSpinner,spnSearchByCity.getSelectedItemPosition());}
What i am doing ::
- I load a fragment(
eg:: Fragment-A
) to the container, and inonSaveInstanceState
event i am storing some data into bundle
i use the code
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putInt("yourSpinner", spnSearchByCity.getSelectedItemPosition());
}
- Now i
replace
thecontainer
withFragment-B
on click of button inFragment-A
i use the code
fragment = FrgMdMap.newInstance(messengerObj);
if (fragment != null) {
getFragmentManager().beginTransaction().replace(R.id.content_frame, fragment).addToBackStack(null).commit();
}
I am successfully able to load the
Fragment-B
Now on orientation change of Fragment-B the
onSaveInstanceState
ofFragment-A
id firing
Questions::
- How is this taking place ?
- How can i make sure this wont happen ?
How is this taking place ?
Actually Fragment-A
is still alive and well underneath your Fragment-B
that is due to the addToBackStack(null)
added when you replaced the fragment.
How can i make sure this wont happen ?
You can remove the addToBackStack(null)
or count the number of stacked fragments in your onSaveInstanceState
method and when it is 0 then you can run the code inside your onSaveInstanceState
EDIT:
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
FragmentManager fm = getActivity().getSupportFragmentManager();
if(fm.getBackStackEntryCount() == 0)
outState.putInt("yourSpinner", spnSearchByCity.getSelectedItemPosition());
}
这篇关于previous片段的onSaveInstanceState的上称为定向电流片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!