Search code examples
javaandroidandroid-jetpack-navigationandroid-deep-link

How to pass safe args as bundle in android explicit deep link?


I am creating an explicit deep link in navigation component using safe args.

PendingIntent pendingIntent = new NavDeepLinkBuilder(getApplicationContext())
                .setGraph(R.navigation.mobile_navigation)
                .setDestination(R.id.nav_order_details)
                .setArguments(//TODO pass bundle)
                .setComponentName(MainActivity.class)
                .createPendingIntent();

The problem is I need to pass a bundle in arguments but I dont know how to convert Safe args to bundle in java.

I found this format in kotlin

OrderDetailsFragmentArgs(id).toBundle()

I need to convert it into java code. Please help.


Solution

  • I finally solved it by using this code

    Bundle bundle = new Bundle();
    bundle.putString("orderId", id);
    
    OrderDetailsFragmentArgs args = OrderDetailsFragmentArgs.fromBundle(bundle);
    
    PendingIntent intent = new NavDeepLinkBuilder(getApplicationContext())
                        .setGraph(R.navigation.mobile_navigation)
                        .setDestination(R.id.nav_order_details)
                        .setArguments(args.toBundle())
                        .setComponentName(MainActivity.class)
                        .createPendingIntent();