<@UJBPFB3SN> May I ask a question about Navigation...
# android
a
@Ian Lake May I ask a question about Navigation Component? Back in the Days where we directly mange
FragmentTransaction
ourselves, we have situation where we need to jump to a certain destination fragment but also have to create the backstack along the way. For example:
A -> B -> C -> D
so in code we do:
Copy code
fun jumpToDFromA() {
    // Assuming in in A
    fragmentManger.beginTransaction()
        .replace(id, B())
        .addToBackStack("state1")
        .setReorderingAllowed(true)
        .commit()

    fragmentManger.beginTransaction()
        .replace(id, C())
        .addToBackStack("state2")
        .setReorderingAllowed(true)
        .commit()

    fragmentManger.beginTransaction()
        .replace(id, D())
        .addToBackStack("state3")
        .setReorderingAllowed(true)
        .commit()
}
With this, I am able to Jump to
D
but also have the backstack
A -> B -> C
. Is this possible to do in
Navigation Component
?
a
(Not Ian Lake) Yeah, deep links simulate the backstack by default I believe https://developer.android.com/guide/navigation/navigation-deep-link
Copy code
val pendingIntent = NavDeepLinkBuilder(context)
    .setGraph(R.navigation.nav_graph)
    .setDestination(R.id.android)
    .setArguments(args)
    .createPendingIntent()
populates the backstack based on the graph
a
But in my experience it only restore the start destination and then the actual destination itself but not the in-betweens. (Or maybe i was doing it wrong?) hmm.. i should probably revisit this
a
Hmm, ok not sure about impl but at least that’s what they claim in their principles: https://developer.android.com/guide/navigation/navigation-principles#deep-link
Deep linking simulates manual navigation
although if you meant a kind of navigation backstack that is not possible with manual navigation, I’m not sure 😅
a
yeah i actually read that as well... imma revisit it and maybe put some update here again..
o
It only restores the start destination from each navigation graph that leads to that destination. Asking on StackOverflow I think Ian Lake will eventually notice the question and rationalize this decision to you. He also hangs out in #compose
i
Just call
navigate
multiple times in a row
😄 3
That's all deep linking is in the end
a
is it safe to call
navigate()
consecutively? is there any other check that needs to be done? As doing
navigate(actionId)
where the
actionId
isnt defined in the current
destination
causes an error.
Also is it correct that when
deeplink
ing only the
startDestination
gets added in the backstack?
i
navigate
updates the state synchronously, so as long as the destination you're going to supports the next
navigate
call, then you're good to go
❤️ 3
You should really, really think hard about building a big synthetic stack at any point. Our UX studies showed that users have pretty negative feelings about having to press the back button many times, particularly when they weren't expecting to have to
💯 2
So yes, when deep linking into your app, the only destinations that get added to the synthetic back stack are the start destinations along the way to the final destination. That means if you had a flat graph structure, users would hit back once to get to your start destination, then exit the app. If you have nested graphs, there could be more than one start destination on that synthetic back stack
a
Alright! Thank you very much! ❤️
d
users have pretty negative feelings about having to press the back button many times
I can relate 😆