Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Access the Bundle from navDeepLinkBuilder.setArguments(bundle)

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")
}

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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
        }
    }
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading