Android 4.2: back stack behaviour with nested fragments

后端 未结 17 1847
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-11-29 16:01

With Android 4.2, the support library got support for nested fragments see here. I\'ve played around with it and found an interesting behaviour / bug regarding back stack an

17条回答
  •  臣服心动
    2020-11-29 16:43

    if you are using fragment in a fragment then use getChildFragmentManager()

    getChildFragmentManager().beginTransaction().replace(R.id.fragment_name, fragment).addToBackStack(null).commit();
    

    if you are using child fragment and replace it use getParentFragmentManager()

    getParentFragmentManager().beginTransaction().replace(R.id.fragment_name, fragment).addToBackStack(null).commit();
    

    if both are not working for you try this getActivity().getSupportFragmentManager()

    getActivity().getSupportFragmentManager().beginTransaction().replace(R.id.fragment_name, fragment).addToBackStack(null).commit();
    

提交回复
热议问题