Got a situation where I got this setup: `RootActiv...
# compose
s
Got a situation where I got this setup:
RootActivity
(which opens ChildActivity) ->
ChildActivity
(which contains a NavHost that I will mention here). And I got some trouble with navigateUp(). More in đź§µ
Inside this navHost, I got this structure:
Copy code
NavHost(startDestination = "A") {
  navigation(
    route = "A",
    startDestination = "First",
  ) {
    composable(route = "First") {
      FirstComposable(
        navigateToSecond = { navController.navigate("Second") { popUpTo("A") } },
        onBack = { navController.navigateUp() || this@ChildActivity.onSupportNavigateUp() || this@ChildActivity.finish() }
      )
    }
    composable(route = "Second") {
      SecondComposable(onBack = { navController.navigateUp() || this@ChildActivity.onSupportNavigateUp() || this@ChildActivity.finish() })
    }
  }
}
Where I want to go to First, from there move to Second, but remove “First” off the backstack, so that from “Second”, if we’ve not been deep linked into there, when they press on the topAppBar back arrow icon,
navController.navigateUp()
doesn’t succeed, onSupportNavigateUp() also doesn’t succeed and it falls back to finish(). When I call onBack() from inside
FirstComposable
, this works fine, and the activity is finished. When I then trigger the
navController.navigate("Second") { popUpTo("A") }
, and from inside
SecondComposable
I call
onBack
, it somehow tried to re-instantiate my ChildActivity instead since navController.navigateUp() does succeed somehow, even though I thought I’d have an empty backstack at that point since I popped it all before going to “Second”. I wonder if there’s something wrong I am doing with my
popUpTo
and how I should be doing this properly in case I am in such a nested graph situation. Or if it’s something else I am messing up
In this case I guess one thing I may be messing up is what I should decide to do with my
popUpTo
. In order to get this “clear backstack, aside from “Second” being in the front” if I should do • navController.navigate(“Second”) { popUpTo(“A”) } • navController.navigate(“Second”) { popUpTo(“First”) } • navController.navigate(“Second”) { popUpTo(“A”) { inclusive = true } } • navController.navigate(“Second”) { popUpTo(“First”) { inclusive = true } } • something else Not sure tbh, and I haven’t found anything in the docs specifying this, only cases where there aren’t nested graphs like this, like here.
I guess what I was facing actually had to do with what
navController.navigateUp()
does and how it works differently when called from the startDestination of a navigation graph, vs when called from another route which is not he start destination. At least that’s what I think I figured out here 🤷‍♂️