7

Navigation component seems just working with short dynamic link

Example: Long Dynamic Link

https://domaindebug.page.link/?link=https://www.website.com&apn=com.x.debug&isi=122...6&ibi=com.ios.x.debug&efr=1

Short Dynamic Link

https://domaindebug.page.link/register
<fragment
        android:id="@+id/fragment_register"
        android:name="com.x.presentation.feature.identification.view.RegisterFragment"
        tools:layout="@layout/fragment_register">
        <action
            ... />
        <argument
            android:name="code"
            android:defaultValue="@null"
            app:argType="string"
            app:nullable="true" />
        <deepLink
            android:id="@+id/deepLink"
            app:uri="https://domaindebug.page.link/register?code={code}" />
</fragment>

If I click on long dynamic link, nothing happens. How can I manage this? I also can't find any documentation about integrate firebase dynamic deeplink + navigation component

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
rafaelasguerra
  • 2,685
  • 5
  • 24
  • 56

1 Answers1

5

I have not seen any official integration of dynamic links with the navigation component so far. But it's pretty simple to integrate them manually by fetching the link with help of FirebaseDynamicLinks and passing it to the NavController:

FirebaseDynamicLinks.getInstance()
            .getDynamicLink(intent)
            .addOnSuccessListener(this) { link ->
                findNavController(R.id.nav_host_fragment).handleDeepLink(Intent().apply {
                    data = link?.link
                })
            }

Please check out the sample project I've created to show the idea. It handles deep links in a separate activity to make UI smoother, but hands over the link to the navigation component for handling.

esentsov
  • 6,372
  • 21
  • 28
  • But why should I declare the long deeplink with navigation components? Without navigation components , I can declare the short link in each activity (Android manifest) and deal it with getDynamicLink – rafaelasguerra Sep 20 '19 at 21:24
  • @rguerra So you want to fetch `https://www.website.com` part from the link and pass it to the navigation library, right? – esentsov Sep 20 '19 at 21:28
  • yes @esentsov, but in navigation components I just have a single activity. Should this activity manage all the deeplinks (FirebaseDynamicLinks) and redirect the user to each fragment? – rafaelasguerra Sep 21 '19 at 19:30
  • @rguerra yes, with the above approach the activity fetches all the deeplinks from `FirebaseDynamicLinks` and passes them to the navigation component to process. That's how navigation library handles the links itself - catches them in the main activity and processes. – esentsov Sep 21 '19 at 19:34
  • But it "passes them to the navigation component" automatically? Or do I need to have a switch/when case for each fragment? I don't see it very clean honestly – rafaelasguerra Sep 21 '19 at 20:41
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/199778/discussion-between-esentsov-and-rguerra). – esentsov Sep 21 '19 at 20:48
  • If you have created the implicit deep link following the documentation, you don't need to add this code. Adding this code will result in the handling of deep links twice. – Mehul Kanzariya Sep 01 '20 at 12:13
  • Using this approach my backstack is gone. I am landing to the fragment but previous fragments are not maintained. Its not even going to the start destination app is just killed after back press @esentsov – Cyph3rCod3r Jan 22 '21 at 13:19
  • @Dr.aNdRO [this link](https://developer.android.com/guide/navigation/navigation-deep-link#implicit) explains when the back stack is maintained and when it's not, might be helpful – esentsov Jan 23 '21 at 15:45
  • @esentsov where to write the above code? inside mainActivity or destination fragment? – Venkat Oct 06 '21 at 07:30