Fragments inside ViewPager not destroyed

In my MainActivity in the onCreate method, I add () MainFragment to FrameLayout main_view_container:

        if (savedInstanceState == null) {
        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
        fragmentTransaction.add(R.id.main_view_container, new MainFragment(), MainFragment.FRAGMENT_TAG);
        fragmentTransaction.commit();
    }

In my MainFragment, I have a ViewPager with a FragmentStatePagerAdapter, and each page is the fragment itself (PagerFragment).

Then at some point after clicking the button, I want to replace the entire MainFragment with another fragment (ReplacementFragment) and add the transaction to the back stack so that I can return to pressing the MainFragment button on the back panel. So I do the following:

FragmentTransaction fragmentTransaction = getActivity().getSupportFragmentManager().beginTransaction();
                fragmentTransaction.replace(R.id.main_view_container, new ReplacementFragment(), null);
                fragmentTransaction.addToBackStack(null);
                fragmentTransaction.commit();

, , MainFragment onDestroyView(), PagerFragments ( ViewPager MainFragment). onStop(). , , MainFragment View onCreateView(), ViewPager, PagerFragments.

, onDestroyView , ?

+4
1

getChildFragmentManager() MainFragment.

+18

All Articles