Android 4.2: back stack behaviour with nested fragments

后端 未结 17 1791
爱一瞬间的悲伤
爱一瞬间的悲伤 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:42

    Seems like a bug. Take a look at: http://code.google.com/p/android/issues/detail?id=40323

    For a workaround I've used successfully (as suggested in comments):

        @Override
    public void onBackPressed() {
    
        // If the fragment exists and has some back-stack entry
        if (mActivityDirectFragment != null && mActivityDirectFragment.getChildFragmentManager().getBackStackEntryCount() > 0){
            // Get the fragment fragment manager - and pop the backstack
            mActivityDirectFragment.getChildFragmentManager().popBackStack();
        }
        // Else, nothing in the direct fragment back stack
        else{
            // Let super handle the back press
            super.onBackPressed();          
        }
    }
    

提交回复
热议问题