What’s the right way to handle `onNewIntent()` when using Compose Navigation? My use case is similar...
c
What’s the right way to handle
onNewIntent()
when using Compose Navigation? My use case is similar to e.g. OAuth authorization flow. I am redirecting back to an Activity via <intent-filter>, and starting the Activity with SINGLE_TOP. There will be a route in the new Intent, which I want to navigate to. Has anyone handled something like this? Here’s some dummy code example of what I mean:
Copy code
class MyActivity {
  
  fun onCreate() {
    setContent { MyApp() }
  }

  fun onNewIntent(intent: Intent) {
    val route = intent.getStringExtra("redirect_route")
    ?? What to do with 'route'
  }
}

@Composable
fun MyApp() {
  val navController = rememberNavController()
  MyNavHost(navController)
}

@Composable
fun MyNavHost(controller: NavController) {
  NavHost(...) {
    ... define routes ...
  }
}
I don’t see how I could access the NavController to trigger navigation, so that doesn’t seem like the right way? I also thought the composition could observe the state of the Activity’s Intent, but that doesn’t seem right either.
a
You don't need it anymore, use compose navigation deeplinks
It Should be in the official docs
c
Hi Allan, thanks for responding. Can you explain some more, what do I not need anymore? I understand about compose navigation deep links, but I don’t see how they apply to this use case.
a
You can check this if this is enough for your requirement https://developer.android.com/jetpack/compose/navigation#deeplinks
c
I don’t think I can apply deep links to solve my problem. The problem I have is that my Activity is being resumed with a new Intent, and I need to handle some information in that Intent, and navigate to a route.
a
But you just pass the route in the intent from your code. Just format the intent like a deeplink instead
c
Ok it took me a moment to see what you mean, but I see how I can use deep links for this. I have it implemented now, but it looks like a new Activity is being started, whereas I want to pop the back stack and return to my previous Activity.
It seems what I want to do may not be possible, and I need to call
handleDeepLink()
explicitly https://developer.android.com/guide/navigation/navigation-deep-link#handle
i
Or use the standard launchMode, where we do the right thing by default
c
Thanks for responding Ian. My use case is kind of like OAuth, where I am opening a custom tab and handling a redirect url. But I don’t want the custom tab activity on the back stack after it’s done. Calling
handleDeepLink()
isn’t the end of the world. Could/should I submit a feature request for handling different launch modes?
i
No, you should always use the default
c
Do you suggest that apps shouldn’t use single top or clear top at all? Personally I took inspiration from how AppAuth implements a similar flow for OAuth: https://github.com/openid/AppAuth-Android/blob/master/library/java/net/openid/appauth/RedirectUriReceiverActivity.java#L54 Is this not recommended?
i
No, you shouldn't need to use any of those at all. Just add your oauth link as a
deepLink
on your destination you want to handle the deep link
☝️ 1
c
Just wanted to follow up that I finally got this working as you guys describe. It took a bit of work because of the different behaviour of starting a new activity vs reusing one via single top. But it works well.
1754 Views