I'm trying to open a fragment with args from my notification. Actually, in my case, I have Audio Player running with Foreground Service with Notification and now I want to navigate to my fragment for that specific Audio by passing Audio Id when a user clicks to the Notification.
How to navigate from Service to a Navigation Fragment using probably Pending Intent?
Asked Answered
You can open your activity with PendingIntent
then open your fragment via handling the Intent
inside of your activity.
or this answer from similar topic
NavDeepLinkBuilder:
val pendingIntent = NavDeepLinkBuilder(context)
.setComponentName(YourActivity::class.java)
.setGraph(R.navigation.your_nav_graph)
.setDestination(R.id.your_destination)
.setArguments(bundle)
.createPendingIntent()
//then
notificationBuilder.setContentIntent(pendingIntent)
this is clumsy when having much navigation. Didn't Navigation Architecture guys make any Utils for this? I mean handling Navigation with Intent easier. –
Deflate
You can look at this post. https://mcmap.net/q/325707/-how-to-open-fragment-page-when-pressed-a-notification-in-android –
Nightwalker
Oh did it! Thanks that should be the answer to my question. –
Deflate
This was really really fast answer, you guys are awesome... thanks again. –
Deflate
This recreates my fragment. Is there a way to have my retained fragment spawned instead? –
Kuopio
I am not able to recreate the backstack with this soultion. I have Fragment A -> B -> C -> D -> E when I use this solution the graph is only A -> E . Any idea? –
Ubiety
Unable to start activity, inside my fragment its showing null pointer exception –
Shaky
I used NavDeepLinkBuilder within my service and pass the service context and It didn't work can you tell how you used NavDeepLinkBuilder in your foreground service. –
Teel
When targeting the new Android S, you have to use
.createTaskStackBuilder() .getPendingIntent(0, FLAG_IMMUTABLE)
instead of .createPendingIntent()
otherwise your app will crash due to the new Pending Intent restrictions. –
Arliearliene How do you create a bundle to pass as an argument? How do you get data from notifications? –
Beltran
© 2022 - 2024 — McMap. All rights reserved.