https://kotlinlang.org logo
Title
a

Archie

07/29/2020, 6:03 AM
@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:
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

allan.conda

07/29/2020, 7:55 AM
(Not Ian Lake) Yeah, deep links simulate the backstack by default I believe https://developer.android.com/guide/navigation/navigation-deep-link
val pendingIntent = NavDeepLinkBuilder(context)
    .setGraph(R.navigation.nav_graph)
    .setDestination(R.id.android)
    .setArguments(args)
    .createPendingIntent()
populates the backstack based on the graph
a

Archie

07/29/2020, 7:58 AM
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

allan.conda

07/29/2020, 8:02 AM
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

Archie

07/29/2020, 8:03 AM
yeah i actually read that as well... imma revisit it and maybe put some update here again..
o

okarm

07/29/2020, 8:32 AM
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

Ian Lake

07/29/2020, 1:55 PM
Just call
navigate
multiple times in a row
😄 3
That's all deep linking is in the end
a

Archie

07/29/2020, 1:58 PM
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

Ian Lake

07/29/2020, 2:06 PM
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

Archie

07/29/2020, 2:14 PM
Alright! Thank you very much! ❤️
d

d4vidi

07/29/2020, 6:13 PM
users have pretty negative feelings about having to press the back button many times
I can relate 😆