3

I have an activity inside an navgraph, directly i am able to navigate to it by below code

Navigation.findNavController(requireView())?.navigate(R.id.sampleActivity)

Please find the navgraph below

<navigation xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/home_nav_graph"
    app:startDestination="@id/appFirstFragment">

    <fragment
        android:id="@+id/appFirstFragment"
        android:name="com.example.deeplinkpoc.AppFirstFragment"
        android:label="fragment_app_first"
        tools:layout="@layout/fragment_app_first" >
    </fragment>
    <activity
        android:id="@+id/sampleActivity"
        android:name="com.example.deeplinkpoc.SampleActivity"
        android:label="activity_sample"
        tools:layout="@layout/activity_sample">
        <deepLink
            android:id="@+id/deepLink2"
            app:uri="https://www.abcdxyz.com/test" />
    </activity>
     </navigation>

But if i try to navigate to it using deeplink its crashing

Process: com.example.deeplinkpoc, PID: 12805
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.deeplinkpoc/com.example.deeplinkpoc.HomeActivity}: android.view.InflateException: Binary XML file line #8: Binary XML file line #8: Error inflating class androidx.fragment.app.FragmentContainerView
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2914)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3049)
    at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
    at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
    at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1809)
    at android.os.Handler.dispatchMessage(Handler.java:106)
    at android.os.Looper.loop(Looper.java:193)
    at android.app.ActivityThread.main(ActivityThread.java:6692)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
 Caused by: android.view.InflateException: Binary XML file line #8: Binary XML file line #8: Error inflating class androidx.fragment.app.FragmentContainerView
 Caused by: android.view.InflateException: Binary XML file line #8: Error inflating class androidx.fragment.app.FragmentContainerView
 Caused by: android.content.res.Resources$NotFoundException: Unable to find resource ID #0x0
    at android.content.res.ResourcesImpl.getResourceTypeName(ResourcesImpl.java:271)
    at android.content.res.Resources.getResourceTypeName(Resources.java:1986)
    at androidx.navigation.ActivityNavigator.navigate(ActivityNavigator.java:200)
    at androidx.navigation.ActivityNavigator.navigate(ActivityNavigator.java:44)
    at androidx.navigation.NavController.navigate(NavController.java:1057)
    at androidx.navigation.NavController.handleDeepLink(NavController.java:732)
    at androidx.navigation.NavController.onGraphCreated(NavController.java:633)
    at androidx.navigation.NavController.setGraph(NavController.java:590)
    at androidx.navigation.NavController.setGraph(NavController.java:555)
    at androidx.navigation.NavController.setGraph(NavController.java:537)
    at androidx.navigation.fragment.NavHostFragment.onCreate(NavHostFragment.java:248)
    at androidx.fragment.app.Fragment.performCreate(Fragment.java:2684)
    at androidx.fragment.app.FragmentStateManager.create(FragmentStateManager.java:280)
    at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1175)
    at androidx.fragment.app.FragmentManager.addAddedFragments(FragmentManager.java:2224) 

UPDATE please find navhost activity xml

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".HomeActivity">
<androidx.fragment.app.FragmentContainerView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/my_nav_host_fragment"
    android:name="androidx.navigation.fragment.NavHostFragment"
    app:navGraph="@navigation/home_nav_graph"
    /></androidx.constraintlayout.widget.ConstraintLayout>

How can i open an activity inside a navgraph with deeplink ?

2 Answers2

1

As per this issue, this is a known issue with Navigation 2.3.2, already marked as fixed for the upcoming Navigation 2.3.3 release.

However:

note that it is never the right approach to attach a <deeplink> to an <activity> destination as that will never give you the right behavior when using an implicit deep link on another app's task (where the system back should immediately take the user back to the app that triggered your deep link). Instead, you should attach your deep link directly to your second activity (either by manually writing the appropriate <intent-filter> or by adding the <deeplink> to the start destination of a nav host in that second activity).

So really, you should move your <deeplink> off of your activity destination, which will both fix your crash (on every version of Navigation) and actually give you the correct behavior. Any validation you need to do needs to be done on the actual correct activity.

ianhanniballake
  • 191,609
  • 30
  • 470
  • 443
  • if i use sampleActivity to open deeplink directly, My UP(top left button) navigation will not take him to previous screen. For UP navigation i want to keep all activities in backstack so i am opening one common activity and navigating to target activity. – LOKESH KUMAR PEDDA Jan 15 '21 at 06:34
  • That just means either 1) you're not calling `super.onSupportNavigateUp()` like [the docs say](https://developer.android.com/guide/navigation/navigation-ui#action_bar) or you haven't [declared a parent activity](https://developer.android.com/training/appbar/up-action#declare-parent). – ianhanniballake Jan 15 '21 at 06:44
0

Make sure these steps are followed:

  1. NavGraph is added to Application class in AndroidManifesto.xml.
  2. Add an ACTION_VIEW to the DeepLink.

If nothing else works, then the following solution will definitely work:

  1. Add <activity> tag in AndroidManifesto for sampleActivity.
  2. Add an implicit intent to this sampleActivity tag, with ACTION_VIEW.
  3. Collect this implicit intent in the sampleActivity's onCreate lifecycle event.

You do not have to rely on Navigation Component to navigate to simpleActivity from the deepLink. You can register the deeplink yourself through implicit intent and it will just work fine.

What NavigationComponent does is it detects the DeepLinks present in the Graph and adds them to the AndroidManifesto. Your problem could originate from the fact that the activity you want to reach, i.e. sampleActivity lives in a nestedGraph. In this case, the deepLink would not be created. However, the destionationId of the sampleActivity will still be known to the NavController. Hence explaining why you are able to reach to sampleActivity from destinationId but not from deepLink.

To make your application aware of sampleActivity deepLink, you would have to propagate this deepLink setup to the parentGraph. Simply initialize the following deepLink in the parentGraph.

 <activity
        android:id="@+id/sampleActivity"
        android:name="com.example.deeplinkpoc.SampleActivity"
        android:label="activity_sample"
        tools:layout="@layout/activity_sample">
        <deepLink
            android:id="@+id/deepLink2"
            app:uri="https://www.abcdxyz.com/test" />
Karan Dhillon
  • 1,186
  • 1
  • 6
  • 14
  • Hi, i don't want to open deeplink activity directly by mentioning in manifest xml. My main activity will receive the deeplink and after some validations it will send it through navgraph. Problem is if i am able to open the activity based on navgraph id, why can't i open same thing with deeplink. – LOKESH KUMAR PEDDA Jan 13 '21 at 05:37