How to perform Multiple back Stack navigation — Developer’s Commute Newsletter

Hitesh Kohli
3 min readDec 8, 2023

--

Photo by Nubelson Fernandes on Unsplash

Multiple Back Stack In Navigation

The flow of an app’s UI is a crucial part. If the flow is not good it will lead to less user retention. Therefore developers should keep flow in mind while developing navigation of the app.

When I was developing an app based on XML, its navigation was not saving the back stack. So, I decided to learn about multiple backstacks and their manual implementation.

In this newsletter, we will explore more about multiple backstacks and how to implement them.

— — —

Do you want these topics in your Email Inbox:

Interesting facts about apps
Expert Advice
Core Concepts
Developer Stories
Apps Reviews
Human error
Code Philosophy

Additional:
- Coding Memes
- Interesting Tweets & Articles
- Motivation

— — —

If you are using newer versions of navigation it automatically saves the backstacks. So, we did not need to implement it manually. But in the older versions, the back stack needed to be implemented manually. Android’s engineers gave us an extension function that could be used with the navigation to save the back stack.

Let’s explore the basics of implementing the backstack manually.

To implement multiple back stacks in the navigation we need to implement a stack for individual tabs in the bottom bar. These individual backstacks maintain their state while we navigate between different screens. That forms the basics of multiple backstacks.

If you want to explore more about its implementation. I would suggest that you watch this video.

https://www.youtube.com/watch?v=i0QEGNotTjM

With the newer version of Android Navigation, we can implement back-stack easily.

In the case of XML :

-In navigation, XML the <action> tags contain attributes that can be used to save Multiple backstacks.

XML Back Stack

— > popupToSaveState — helps us to pop up the navigation stack up a destination desired

— > popUpTo — Saves the destination which popup To Save State pops

— > restore State — It restores the previous saved state

— > destination — It restores the destination defined in this tag

In the case of Jetpack Compose:

-In navigation, Jetpack compose contains options that can be used to save Multiple backstacks

Jetpack Compose Back Stack

— > popUpTo — It finds the Start destination of the navigation

— > saveState — Saves the state of the first destination

— > restore state — It restores the previously saved state

— > launching Single Top — It helps us avoid multiple copies of the same composable

This is for today I hope enjoyed this newsletter. If you enjoyed it please let me know and if I have written something wrong please forgive me.

Thank you for reading

Regards,

Developer Commute’s Captain

— — —

Do you want these topics in your Email Inbox:

Interesting facts about apps
Expert Advice
Core Concepts
Developer Stories
Apps Reviews
Human error
Code Philosophy

Additional:
- Coding Memes
- Interesting Tweets & Articles
- Motivation

— — —

--

--

Hitesh Kohli

I will help you build distribution with apps |Building and Designing apps| Writing Newsletters 📩| Building @Developcommute & Niwa