How to Preserve Screen State Across Multiple Navigation Stacks
A guide to managing UI state in independent back stacks with Navigation 3.
1The Multi-Tab Navigation Challenge
Modern mobile apps require separate navigation histories for each tab. However, local UI state (e.g., search queries) can be lost during tab switches. This article explains how Jetpack Navigation 3 solves this with rememberDecoratedNavEntries.
2Limitations of rememberSaveable
While rememberSaveable preserves UI state during configuration changes, it falls short for multiple navigation stacks. Traditional approaches lose state during tab switches because state management is tied to a single composition.
3New Approach: Decorated NavEntries
Navigation 3's rememberDecoratedNavEntries API creates state scopes for each navigation entry. This allows independent preservation of states, like a search bar in Home tab versus Search tab.
4The Role of NavEntryDecorator
The rememberSaveableStateHolderNavEntryDecorator creates separate state storage for each NavEntry. Screens can continue using standard rememberSaveable regardless of their navigation stack.
5High-Level vs Low-Level APIs
Navigation 3 offers both high-level APIs like NavDisplay(backStack) for simplicity and low-level control via rememberDecoratedNavEntries. Developers can use low-level APIs for multi-stack scenarios to build more explicit architectures.
6Result: Clean Architecture
This approach separates UI logic from navigation state management, enabling more maintainable code. Screens focus solely on UI while state management becomes automatic via NavEntryDecorators. An ideal solution for complex multi-tab applications.
Original article: ProAndroidDev - Medium — Çoklu Navigasyon Yığınlarında Ekran Durumu Nasıl Korunur?