Monday, 21 June 2021

Why argument is "null" when passed using a pendingIntent using Navigation Component?

I am using the Navigation Component and I am trying to trigger an explicit deep linking to a specific destination, represented by a Fragment, when the user taps on a notification.

According to the documentation a pending intent can be create like this:

val bundle = bundleOf("id", "1234")

val pendingIntent = NavDeepLinkBuilder(context)
    .setGraph(R.navigation.nav_graph)
    .setDestination(R.id.myDestination)
    .setArguments(args)
    .createPendingIntent()

Where nav_graph is defined as follow:

<fragment 
   android:id="@+id/myDestination"
   android:name="MyFragment">

   <argument
      android:name="id"
      app:argType="string" />

</fragment>

I would then use the pendingIntent into the notification using the NotificationCompat.Builder with:

.setContentIntent(pendingIntent) 

When I tap on the notification the right destination is actually opened, but the value args.id would be "null" (not null, but a string with "null" value. In my fragment I have

private val args by navArgs<MyFragmentArgs>()

...

override fun onCreate(saveInstanceState: Bundle?) {
   args.id // The string value is "null". 
} 

NOTE: nav_graph is embedded in another navigation graph.



from Why argument is "null" when passed using a pendingIntent using Navigation Component?

No comments:

Post a Comment