Android Navigation: Multiple Back Stacks Support

Rina Thakkar
Mindful Engineering
3 min readSep 15, 2021
Image Credit: Android Developers

As we know that most of the android apps in the market have bottom navigation to navigate the user from one screen to another screen. And it is bit tricky to manage back stack for each tabs while using bottom navigation.

Navigation is a key part of the application to navigate the users but sometimes when the user navigates from one screen to another screen with bottom navigation they lose their state because the multiple back stack support is not provided or we can say the state of the user is not handled perfectly and ultimately the user gets a bad experience, they might uninstall your app quickly.

So as we are android app developers it is our responsibility to handle the user’s state properly to give them the best user experience. So in this article, we will learn how to handle navigation properly with bottom navigation.

Let’s say your app uses Bottom Navigation View. When the user navigates one tab to another tab, the back stack for the current tab will be saved and the back stack for the selected tab will be reinstated seamlessly.

Video Credit: Android Developers

We can use setupWithNavController() method to connect BottomNavigationView with NavController from NavigationUI.MainFeed

We can combine two separate navigation graphs into a single graph by using the include tag. Now we have to just add that single graph in the activity’s layout of the NavHostFragment tag.

<navigation xmlns:android="http://schemas.android.com/apk/res/andWithoutBackStacks_1WithoutBackStacks_1roid"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/main_bottom_nav"
app:startDestination="@id/main_feed">

<include app:graph="@navigation/main_feed" />
<include app:graph="@navigation/post" />
</navigation>

If we are using the old version of navigation and not enabling the multiple back stack and checked the app, we can see fragment’s/back stack state is lost and we should keep the fragment’s/back stack state to give a seamless user experience while switching to other tabs. So we need some solution for that.

We have the below solution for the above issue.

As we know android developers provided another way using the NavigationExtension file.

Now, you can check it. We are adding a navigation extensions file and with these extensions, the app keeps a separate NavHostFragment, has its own back stack for each tab while switching from one tab to another.

And after upgrading the version of navigations with “2.4.0-alpha01 or higher” and remove those extension functions and again we run the app and checked bottom tabs, we can see that bottom tabs can maintain their state and reinstate their back stack as we switch to other tabs. Notice this behavior is enabled by default.

We are using NavigationView.setupWithNavController() which uses the new restoreState and saveState NavOptions by default whenever they would pop the back stack.

If you also need to manage multiple back stacks, just update your navigation and fragment dependencies & it's done. No need to do it manually by writing boilerplate code.

Yeah….this is done !!

If you found this article helpful then please give your valuable claps with feedback.

Happy Learning.

--

--