In handling a deep link in Compose navigation I'm ...
# compose
n
In handling a deep link in Compose navigation I'm seeing a couple of strange things that I can't figure out. First is the deep link seems to be delivered to the app twice. Second, the backstack seems to be recreated properly, however, the ViewModel I've linked to the sub navigation gets recreated when the app handles the deep link. That's really unexpected. I feel like I must be doing something very wrong at this point. Any ideas?
i
You'll always get a fresh activity task stack in the correct state when handling a deep link with Navigation
How are you testing your deep link?
n
We have an authentication flow that ends up with a link that has a custom scheme, this is primarily a web flow, two legged oauth, I think, and this results in a 'code' being sent in a deep link back to the app which we have to exchange for a token.
We have part of our navigation graph that looks like this. It's just implemented in our single activity right now.
Currently the app uses this to continue with the verification of the oauth code. Using the hilt navgraph viewmodel for added complexity. It can be shown that on return into the app through the deep link, the viewmodel is not the same instance we had before. We aren't yet handling state saving on that viewmodel... we probably need to I guess.
At some point, I made a test app to see if these issues were something we did in our current app, or if it was happening in a green field app. And it seems to be consistent. If you think it would be helpful I could share that sample app, it has the deep linking performed by launching a sample web page from assets.
i
I think it is all working as intended as per my first comment. Whenever a deep link is triggered from outside of your app (i.e., with the
NEW_TASK
flag), the entire activity task stack (i.e., your one activity and everything in it) is cleared and replaced with a brand new instance. This means that any deep linking handling is completely consistent whether the user has swiped away your task from the Recents menu, if they've swapped back to your app manually and navigated to other destinations or to other activities, etc.
This is explained in more detail on the implicit deep link docs: https://developer.android.com/guide/navigation/navigation-deep-link#implicit
But the implication is that when that deep link is triggered, you need to have enough information to proceed as if none of your previous destinations have ran (because, in your new activity instance, they haven't)
n
Okay, I guess that makes sense. I was anticipating magic. But, instead get reality. Thanks for taking a look, Ian. Appreciate it.
127 Views