Issue
I'm using Android Navigation UI for navigation in my app. I need to open a web URL (dynamic links) in my second fragment.
this is my code in the navigation graph
<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/mobile_navigation"
app:startDestination="@+id/nav_home">
<fragment
android:id="@+id/nav_home"
android:name="com.myproject.main.HomeFragment"
android:label="Home"
tools:layout="@layout/fragment_home" >
<action
android:id="@+id/action_nav_home_to_secondFragment"
app:destination="@id/secondFragment" />
</fragment>
<fragment
android:id="@+id/nav_library"
android:name="com.myproject.main.SecondFragment"
android:label="@string/menu_library"
tools:layout="@layout/fragment_library">
<deepLink
android:id="@+id/deepLink"
app:uri="https://myproject.page.link/*" />
</fragment>
</navigation>
And added this navigation graph to my main activity in the manifest
<activity
android:name=".main.MainActivity"
android:configChanges="keyboard|keyboardHidden|screenLayout|screenSize|orientation"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar"
>
<nav-graph android:value="@navigation/mobile_navigation" />
</activity>
I want to open my second fragment when the user clicks on a URL like https://myproject.page.link/i5sA
but when I click on this link my app is even not prompted to open in this app. Only browsers are prompted. when I click on open with chrome browser it just opens my app with the default start destination.
what I need is when user press on that web URL I need to open my app MainActivity with SecondFragment and get that URL data instead of opening the default home fragment.
I had seen the developer page and a lot of articles and tutorials but I don't know what I'm missing here, anyone can please tell me...
Solution
app:uri="https://myproject.page.link/*"
Using *
as a placeholder is not a valid deeplink in android architecture navigation.
Although the https://myproject.page.link/*
URI is valid to open https://myproject.page.link/* exactly as-is (i.e. with the asterisk)
But as per documentation:
Path parameter placeholders in the form of {placeholder_name} match one or more characters. For example, http://www.example.com/users/{id} matches http://www.example.com/users/4.
So, if you want to make this asterisk as a placeholder for some info to be passed to your app, you need to wrap this into curly braces {}; so your URI would be:
app:uri="https://myproject.page.link/{placeholder_name}"
Notice that you can test this URI from the Google search app (not the browser app), because if you want to open it from the browser you have to register the domain name globally; this is to allow Google verify that you are the owner of the URI. Check here for more info.
Answered By - Zain
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.