1

In my app's navigation I call saveState and restoreState to have a smooth user experience and not load the screens again and again everytime the user navigates. But in one scenario I don't want this behaviour. Whenever one user blocks another it call's popBackStack() to leave the blocked users profile. What I want now is that whatever screen he ends up after popBackStack() it gets reloaded and NOT restored. Because I want to hide all the content of that blocked user from every list in my app.

Right now after navigating back from the blocked users profile it still shows this users content inside the list because the screen state is restored with outdated data.

What I'm looking for is a way to delete all saved states programmatically. Something like navController.clearAllSavedStates() would be a dream.

EDIT Calling navController.popBackStack(saveState = false) as suggested is not working because it tells me "Cannot find a parameter with this name: saveState"

HavanaSun
  • 446
  • 3
  • 12
  • 39
  • I think this will help you https://stackoverflow.com/questions/50514758/how-to-clear-navigation-stack-after-navigating-to-another-fragment-in-android – Xeyal Nov 09 '22 at 10:22
  • @XəyalŞərifli not sure how using popUpTo will help me. All I can call is .popBackStack() because the User profile is accessible from all around the app. So I cannot tell where the user ends up after popBackStack(). I edited my questions and I'm asking now for Jetpack Compose specifically – HavanaSun Nov 09 '22 at 10:36

1 Answers1

2

If the data you don't want to show are saved on the related user backstackentry try:

 navHostController.popBackStack(route ="some route on backstack", inclusive= false saveState = false)

It should drop all the saved state unless you have some of these data saved on other stack entry or activity

zjmo
  • 625
  • 2
  • 8
  • Cannot find a parameter with this name: saveState – HavanaSun Nov 20 '22 at 23:25
  • There is no popBackStack function which only takes saveState as an parameter – HavanaSun Nov 20 '22 at 23:28
  • @HavanaSun yes i've updated it – zjmo Nov 21 '22 at 06:49
  • But what to put as "some route on backstack" if I dont know which one the user will end up after popping the backstack? he can access the UserProfileScreen from every screen across the whole app so it could be literally any screen that he ends up after calling .popBackStack() – HavanaSun Nov 21 '22 at 10:01
  • @HavanaSun if the viewmodel is scoped to the user route even if you do a simple popBackStack() without arguments should reset the data of that viewmodel. If you have other data in another screen or viewmodel youll have to refresh it. In general each screen should have the same single source of data so that when you update it from one screen it will be reflected in the others – zjmo Nov 21 '22 at 10:11
  • Yes the thing is after blocking a User I don't filter all the lists in my app for this blocked users content. That would not be possible for me. The simples way is just to simply fetch the list again from the server where it filters all the content of the blocked user on the backend site. I just need a way to call my LaunchedEffect() again after calling .popBackStack – HavanaSun Nov 21 '22 at 10:16
  • The LaunchedEffect(Unit) fetches the list from the Database. But LaunchedEffect is not getting called when the Screen is in a savedState – HavanaSun Nov 21 '22 at 10:17
  • 1
    Well think if you have another state variable that changes when the used gets blocked you could use in LaunchedEffect(state) so that everytime it changes, the sideeffect gets relaunched. Is there a way to listen for changes in the database on the server? in that case would much easier to fetch the results in a viewmodel everytime the server notifies – zjmo Nov 21 '22 at 11:40
  • Like @zjmo said, and I think I commented this to one of your post, "I just need a way to call my LaunchedEffect() again after calling .popBackStack" - you should probably consider providing a `key` that will change for your `LaunchedEffect` to `re-trigger` and the logic of changing it is bound to your "popBackStack" use-case. As far as I understand compose navigation, composables in the graph are persisted, NavHost will not "re-create/restart" the composables for their `Unit-keyed` LaunchedEffect to re-trigger. – z.g.y Nov 22 '22 at 00:21