编程知识 cdmana.com

Fragment reconstruction in Android template bottom navigation activity

Bottom Navigation Activity In the switch Fragment when , Every time we destroy and rebuild , In need of retention Fragment You can't keep data when you're on .

open MainActivity Layout file for ,NavHostFragment.

android:name="androidx.navigation.fragment.NavHostFragment"

stay NavHostFragment Source code onCreate Among the methods

onCreateNavController(mNavController);
 protected void onCreateNavController(@NonNull NavController navController) {
        navController.getNavigatorProvider().addNavigator(
                new DialogFragmentNavigator(requireContext(), getChildFragmentManager()));
        navController.getNavigatorProvider().addNavigator(createFragmentNavigator());
    }
protected Navigator<? extends FragmentNavigator.Destination> createFragmentNavigator() {
        return new FragmentNavigator(requireContext(), getChildFragmentManager(),
                getContainerId());
    }

You can see , Every time you create NavController when , Will be recreated fragmentNavigator

版权声明
本文为[Sad Warrior]所创,转载请带上原文链接,感谢

Scroll to Top