adrielcafe / voyager

🛸 A pragmatic navigation library for Jetpack Compose
https://voyager.adriel.cafe
MIT License
2.27k stars 109 forks source link

State Preservation Issue with Nested Navigators in Voyager #383

Open pitampoudel opened 1 month ago

pitampoudel commented 1 month ago

I am currently working on a project that uses Voyager for navigation. My application has a hierarchical structure of navigators and screens, which is causing an issue with state preservation.

Here’s the structure of my navigators and screens:

A root Navigator is present at the main level. Inside this root Navigator, there is a HomeScreen. Within the HomeScreen, I have another TabNavigator which contains three tabs: TabA, TabB, and TabC. In TabA, I have constructed a TabAScreenModel using the getScreenModel() composable function. This TabAScreenModel is provided by Koin.

The issue arises when I navigate from HomeScreen (which is currently displaying TabA) to another screen (ScreenX) using the parent navigator. ScreenX is not inside the TabNavigator, but is instead a part of the root navigator.

When I navigate back to HomeScreen from ScreenX, the TabAScreenModel associated with TabA is destroyed and re-constructed. This results in the loss of its previous state.

I would like to preserve the existing state of TabAScreenModel when navigating away from HomeScreen and then returning to it. Any guidance or suggestions on how to achieve this would be greatly appreciated.

creativedrewy commented 1 month ago

I've encountered this as well, by the way. I have Voyager pretty deeply integrated into my app, so I may just end up having to submit a fix PR myself here sometime soon.

shpasha commented 1 month ago

Have you tried setting disposeNestedNavigators = false in the root navigator?

pitampoudel commented 1 month ago

@shpasha Yeah, that worked for me. But It would be great if there was documentation on why this parameter is for

creativedrewy commented 1 month ago

@pitampoudel how are you obtaining the parent navigator? I am setting the disposeNestedNavigators = false flag and still losing the state from inside my tab when navigating. I'm wondering if I'm getting it the wrong way.

pitampoudel commented 1 month ago

To obtain the parent navigator I am using this => val parentNavigator = LocalNavigator.currentOrThrow.parent and is working for me. @creativedrewy