voyager icon indicating copy to clipboard operation
voyager copied to clipboard

State Preservation Issue with Nested Navigators in Voyager

Open pitampoudel opened this issue 1 year ago • 6 comments

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.

pitampoudel avatar Mar 29 '24 07:03 pitampoudel

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.

creativedrewy avatar Apr 05 '24 04:04 creativedrewy

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

shpasha avatar Apr 05 '24 17:04 shpasha

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

pitampoudel avatar Apr 07 '24 00:04 pitampoudel

@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.

creativedrewy avatar Apr 08 '24 18:04 creativedrewy

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

pitampoudel avatar Apr 09 '24 11:04 pitampoudel

@shpasha how exactly has this be done? I have exactly the similar structure, and when opening ScreenX for full screen in root navigator, tabs are hidden, after returning back to tabs, all viewmodels are recreaded. As a workaround I store screenViewModels in RootNavigator

naixx avatar Jul 18 '24 21:07 naixx