Android NavigationUI with multiple stacks: How to keep state of the section roots?

I have built a working multi stack implementation of NavigationUI with 5 sections, each has its own backstack.
The remaining problem i have is that for each section root fragment onSaveInstanceState is not called when navigating to another section. It is called for every other fragment in the back stack of each section.

I use this code to navigate between sections:

NavOptions navOptions = new NavOptions.Builder()
        .setLaunchSingleTop(true)
        .setRestoreState(true)      
        .setPopUpTo(currentDestination, true, true)
        .build();
navigationController.navigate(targetDestination, null, navOptions);

If i add a “dummy” fragment as root for each section the real root gets onSaveInstanceState called, but that would require all kind of hacks with back navigation.

Leave a Comment