androidmvvmandroid-architecture-componentsandroid-architecture-navigationnavigation-architecture

How to navigate from Service to a Navigation Fragment using probably Pending Intent?


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.


Solution

  • 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)