When navigating to a deeplink is there a way to pr...
# compose-destinations
r
When navigating to a deeplink is there a way to pre-populate the backstack or i have to do this manually ? example: A ->B ->C where we are on screen A and navigating with deeplink to C can C have B parent with some automagic configuration?
s
navigateUp does this automatically in the base library provided your nested graphs look the way you describe. Read more here https://developer.android.com/guide/navigation/principles#the_up_button_never_exits_your_app
πŸ‘€ 1
r
I think im missing something I am on MainScreen and trying to deeplink to GraphADestinationB in GraphA where it should be MainScreen -> GraphADestinationA -> GraphADestinationB .navigateUp pops to MainScreen when deeplinking to GraphADestinationB where GraphADestinationA is starting point of GraphA
s
And you're doing navController.navigateUp() when you're experiencing this behavior? Probably a good idea to paste your graph setup here too so we can also take a look at it
r
yeah ive just implemented
Copy code
BackHandler {
        destinationsNavigator.navigateUp()
    }
in GraphADestinationB. Give me a minute to "shadow" it a little
s
Btw perhaps a bit unrelated, maybe you're doing this for testing purposes, but you shouldn't be calling navigateUp when someone presses back. Only when they press "up" from the top left of the screen. You may already know this, but I felt like saying it because I was the one who suggested it in the first place πŸ˜…
πŸ‘ 1
r
alright so it looks like this i am in need to deeplink to SubgraphADestinationB and back to start point of SubgraphA. this might be releated to that SubgraphADestinationB should be nested "below" start point of SubgraphA ? navigatingUp pops to MainGraph starting point which is GraphB lets say as up from the docs the start point of subgraph should be added to the backstack
s
Hmm, I don't think it would have to be under the the start point of SubgraphA. Your graph looks correct to me πŸ‘€ Up should go to SubgraphA, which should realize SubgraphADrstinationA is the start destination and it should go there. Can you reproduce this in a simple project? Perhaps with/without compose-destinations too, to rule out that something is wrong there.
πŸ‘ 1
r
I will try to do that but most likely knowing me in new project everything will work great πŸ˜… cant do that now right now though. Ill try to do that as soon as possible thanks!
πŸ˜… 1
s
Alright now I understand what was going on! NavigateUp does create a synthetic backstack when you've deep linked into it, that is true, but there is one super important distinction here! It only does that when you are in your app in another app's task! If you are inside your app normally, and you just navigate somewhere, even if it's "with a deep link", that is treated like a normal navigation, (I think in fact any navigation is treated as a "deep link" navigation if you dig in the internals), so you just add that one item in the backstack. So navigateUp() and popBackStack() just do the same thing in these scenarios. The docs read:
Copy code
When your app is launched using a deep link on another app's task, Up transitions users back to your app's task and through a simulated back stack and not to the app that triggered the deep link. The Back button, however, does take you back to the other app.
So if you do actually open this app using a deep link specifically, but from inside another app, then you should get the correct behavior of navigateUp taking you to SubgraphADestinationA
☝️ 1
r
Seems like this is correct, thanks for answering and i guess if i want to launch a deeplink in app id have to start a new intent to actually create the synthetic backstack instead of just passing a deeplink into the navController
s
Hmm of you must do that inside your app itself, you could build the entire backstack yourself too by doing all the navigations together. And on the first navigation probably pop the entire backstack off. But in those cases navigateUp and popBackStack would probably behave the same, since you're in your own task again
r
id like to skip that unnecessary work. after a quick test doing this
Copy code
val intent = Intent(Intent.ACTION_VIEW)
intent.setData(Uri.parse("<myapp://screenB>"))
startActivity(intent)
instead of
Copy code
navController.navigate("<myapp://screenB>".toUri())
creates a proper synthetic backstack so far, animation is a bit clunky after a first navigateUp but it saves a lot of manual backstack building setting
Copy code
android:launchMode="singleInstance"
in a target activity fixes clunky animation which in my case results in correct app behaviour
s
I will warn against singleInstance since it breaks other interactions that apps normally do when deep linked to etc. But if you're okay with that tradeoff then it looks all good 😁
πŸ™Œ 1