Hello ! Do you know if it’s possible to destroy an...
# compose
i
Hello ! Do you know if it’s possible to destroy and recreate a NavController ?
Copy code
val navController = rememberNavController()
rememberNavController()
does not allow to pass keys
z
You should be able to use the
key
function
s
Although it would be interesting to know why you want to do this in the first place.
4
u
Generally, since the NavController plays the role of managing navigation and the back stack between the destinations declared within the NavHost, recreating it is not the correct approach. If you want to manage the screen's stack, you can do so through the NavController connected to the NavHost.
i
So my app after a loonng time in background expires couple of things (like selected profile) that are needed by the app to make requests for instance. When the app comes back in foreground, it tries to restore all these things and obviously fails. We so would like to “reset” navController to start at splash and also clear all the saved infos it has for destinations. This led us to think recreate the NavController is the easiest way to achieve this.
s
Sounds like you can observe that situation outside of your NavHost and call popUpTo your
navController.graph.findStartDestination().id
to do the same rather than recreating your entire NavController. Although for an optimal user experience, I'd expect to be able to login again or whatever it is, and then be resumed back to exactly where I was at
u
i
In the end I found a solution to avoid having doing that, it’s cleaner, thank you everyone for helping
s
Person A: How do I do this? Person B: Try this Person C: Try that Person D: Try this instead Person A: Thanks all, I found a different solution to my problem but I won't tell you which one it was, thanks all. Many such cases 😅
i
Sorry I can describe it, but it has nothing to do with my exact question ... It’s something very specific to my app. Navigation order to re-trigger Splash was sent before the compose NavHost was composed. What I wanted initially was recreate the controller which seemed an easy fix. Finally I made a synchronization to make sure the order is sent after composition, fix is much more complex but way cleaner. No so easy to explain without being deep dived in the code. What I can say is, I didn’t find a solution for what I asked initially
s
So are you still recreating your entire NavController in those cases, after the synchronization and everything?
i
nope I made it without needing to
s
So... what do you end up doing to clear the backstack after all? Not recreating the NavController. Not doing pop up to navController.graph.findStartDestination().id. So something else? I am just quite curious since I already started thinking about this problem after reading your question and it would make me happy to hear what you actually did 😄
i
haha, so basically, in our app we have actions that trigger navigations (flows). In our ViewModel that would trigger that navigation we wait both for the subscription from the NavHost and for Profile domain to notify us that it is expired. When both conditions are met we trigger our navigation to Splash.
👍 1