Issue
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")
}
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
}
}
}
Answered By - Dwane13
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.