previous片段的onSaveInstanceState的上称为定向电流片段 [英] onSaveInstanceState of previous fragment is called on-orientation of current fragment

查看:127
本文介绍了previous片段的onSaveInstanceState的上称为定向电流片段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在做什么


  • 我打开一个片段(例如::片段-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 in onSaveInstanceState 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 the container with Fragment-B on click of button in Fragment-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 of Fragment-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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆