I noticed with NavHost (using 2.4.0-alpha06) that ...
# compose
j
I noticed with NavHost (using 2.4.0-alpha06) that if I create a PendingIntent.getActivity on an activity with a data URI, for some reason it creates an activity twice when launching from the background with no currenty activity open. The first of them will even be destroyed instantly (or perhaps one activity launches the other one for some reason?). Once I remove the NavHost from my compose setup this no longer happens. Is this intended behavior? Or am I doing something wrong? Also, this setup doesn't appear to handle onNewIntent. How could I fix this?
Command to trigger the behavior:
adb shell am start -a android.intent.action.VIEW -n test.joey.deeplink_test/test.joey.deeplink_test.MainActivity -d "<test://b>"
c
Have you tried creating your PendingIntent from TaskStackBuilder? https://developer.android.com/jetpack/compose/navigation#deeplinks
Also, see this note about launchMode when handling deep links: https://developer.android.com/guide/navigation/navigation-deep-link#handle
I asked a related question recently in case it is of interest to you: https://kotlinlang.slack.com/archives/CJLTWPH7S/p1627073470423800
j
Yes, the weird thing is that the double activity creation still appears to happen, not sure if that's something to worry about or if it's intended and safe behavior 🤔 (on a note that this only happens when data uri is set, when only extras/data is set this doesnt happen weirdly)
c
What happens if you remove this from your activity definition in the manifest:
Copy code
android:launchMode="singleTask"
j
Made some changes, don't remember what I changed, but apparently it works now, thanks for the aid :P
👍 1
One more question: Did you manage to fix that once you open a deeplink (intent with data uri), that it creates a new instance of the activity and removes the previous one? I'd like to have deeplink navigation to navigate within the existing activity compose backstack if there's already an activity open (either in foreground or background) 🤔
c
No it’s not possible to reuse an existing Activity from the back stack automatically. You can handle it manually yourself if you want to:
Copy code
override fun onNewIntent(intent: Intent?) {
    super.onNewIntent(intent)
    navController.handleDeepLink(intent)
}
j
Guess I'll have to handle it myself in that case, kind of annoying if you're editing a form or something, open a deeplink and all progress is gone as the deeplink resets everything. Perhaps I'm missing something here, but I'll just take the "easy" way for now and handle it myself
c
I suspect that the argument (from Google’s perspective) is that your Activity and views should save their state in onSaveInstanceState, so that they can be restored in another instance of the Activity later. This is useful in general, since your Activity could be destroyed for all sorts of reasons.
j
Yeah I think I'm missing something with how the task stack works or something. Before compose I just used a single activity with singleInstance launchmode. All deeplinks were delivered to the same activity which handled it fine, but somehow with compose it resets the activity, even if it's already open it just recreates it, so perhaps I should invest into properly understand how tasks work 🤔
c
Fwiw with compose it seems you can build an entire app within a single Activity. You would need to start a new activity, for instance, to get a result, or perhaps open a webview or similar. But aside from that, in most cases, you can get away with a single Activity. Is there a reason you need two Activities?
j
I actually have only a single activity, but somehow receiving an intent with a data URI seems like to clear the current activity and start a new one 🤔 It's currently like this: - Launch app to MainActivity which has startdestination A - Add route B to backstack - Add route C to backstack - Current backstack is C > B > A, so 1 backpress goes to B, one more to A, one more finishes the activity - Open deeplink to D using the TaskStackBuilder, somehow creates a new resetted activity. Pressing back will go to A as thats the startDestination and the resetted activity added D on top of that, but I expect to go back to C > B > A