Access the Bundle from navDeepLinkBuilder.setArguments(bundle)

86
May 02, 2022, at 11:50 AM

There is a Service that at some point creates a Notification that always leads to the same Fragment. The Service wants to send the Fragment some Key-Value Data. In an Intent I would put those as Extras. The PendingIntent doesn't have a putExtras method, but the NavDeepLinkBuilder has a setArguments method that takes a Bundle.

val pendingIntent = NavDeepLinkBuilder(applicationContext)
            .setComponentName(MainActivity::class.java)
            .setGraph(R.navigation.nav_main)
            .setArguments(myExtras)
            .setDestination(R.id.destinationFragment)
            .createPendingIntent()
val builder = NotificationCompat.Builder(this, CHANNEL_ID)
            .setPriority(NotificationCompat.PRIORITY_MAX)
            .setContentText("some text")
            .setContentTitle("some text")
            .setContentIntent(pendingIntent)
startForeground(NOTIFICATION, builder.build())

Can the bundle that was fed into setArguments be accessed later from the destination Fragment? I tried it in the following way, but it just returns me the default value:

activity?.intent?.extras?.let {
    val myExtra = extras.getInt(KEY, DEFAULT_VALUE)
    Timber.e("got the Value $myExtra")
}
Answer 1

Yes, you can access your bundle from your destination fragment. Check this out

//your fragment
override fun onCreateView(
    inflater: LayoutInflater, container: ViewGroup?,
    savedInstanceState: Bundle?
): View {
    super.onCreateView(inflater, container, savedInstanceState)
    if (arguments != null) {
        if (requireArguments().containsKey(YOUR_KEY)) {
            //logic
        }
    }
}
Rent Charter Buses Company
READ ALSO
TextView Marquee is not working in linearlayout

TextView Marquee is not working in linearlayout

Unfortunately, I cannot deal with this problemMore precisely, the marque function does not work for me after adding a TextView to LinearLayout

100
How can I expand a short URL, so I can get the full expanded URL in a variable?

How can I expand a short URL, so I can get the full expanded URL in a variable?

I want to expand a short URL and store the long URL in a variable without accessing that link with the browserAny ideas how I can do that in kotlin?

94
Google Fit API not returning activities from last 90 days

Google Fit API not returning activities from last 90 days

I'm trying to retrieve the Google Fit activities from the last 90 days, but it is not returning all the activities

73
OnActivityResult method is deprecated, what is the alternative?

OnActivityResult method is deprecated, what is the alternative?

I recently discovered that onActivityResult is deprecatedWhat should we do to handle it?

164