Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jetpack Navigation deeplink with query parameters

it looks like it isn't possible to process a deeplink with query parameters in the new Jetpack Navigation library. If you put the following to the navigation.xml: <deepLink app:uri="scheme://host/path?query1={query_value}" /> then the deeplink does not open the fragment.

After some digging I found that the culprit is probably in the NavDeepLink when it transforms the url from xml to a Pattern regex. Looks like the problem is a question mark that is not excaped.

I wrote a test which fails:

@Test
fun test() {
    val navDeepLink = NavDeepLink("scheme://host/path?query1={query_value}")
    val deepLink = Uri.parse("scheme://host/path?query1=foo_bar")
    assertEquals(true, navDeepLink.matches(deepLink))
}

To make the test pass all I have to do is to escape the ? as following:

@Test
fun test() {
    val navDeepLink = NavDeepLink("scheme://host/path\\?query1={query_value}")
    val deepLink = Uri.parse("scheme://host/path?query1=foo_bar")
    assertEquals(true, navDeepLink.matches(deepLink))
}

Am I missing something really basic here to pass query values to my Fragment or is this not supported feature at the moment?

like image 600
bakua Avatar asked Jun 16 '18 10:06

bakua


People also ask

How do I configure deeplink?

Adjust Deeplink Generator To use the tool, log in to your Adjust dashboard and open the Menu, where you'll see the 'Deeplink Generator' as an option. Click to open, and you'll find a page to input the information required to create your deep link. Then simply copy and paste into whichever campaign you've set up.

What is the difference between deep linking and deferred deep linking?

In the context of mobile apps, deep linking consists of using a uniform resource identifier (URI) that links to a specific location within a mobile app rather than simply launching the app. Deferred deep linking allows users to deep link to content even if the app is not already installed.

What is deeplink redirect?

Deep links send mobile device users directly to relevant pages in your app rather than your website. Users click on ads and go directly to your app pages. You can use deep links in many Google Ads products, including App campaigns for engagement, App dynamic remarketing, and Search, Shopping, and Display campaigns.


1 Answers

You need to add DeepLink Navigation to AndroidManifest.xml ( special Activity that handles the fragment) so when deeplink clicked your app can receive the DeepLink and pass it to that navigation and fragment & can read it as argument:

I'll put Kotlin codes here :

In your navigation file, your fragment that gonna handle the deeplink with arguements must be like this:

<fragment
        android:id="@+id/menu"
        android:name="ir.hamplus.fragments.MainFragment"
        android:label="MainFragment">

    <action android:id="@+id/action_menu_to_frg_messenger_main" 
     app:destination="@id/frg_messenger_main"/>

    <deepLink app:uri="http://hamplus.ir/request/?key={key}&amp;id={id}" />

    <argument android:name="key" app:argType="string"/>
    <argument android:name="id" app:argType="string"/>

</fragment>

read deeplink arguments in frasgment /Activity :

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)
  //Or in activity read the intent?.data
       arguments?.let {
            Log.i("TAG", "Argument=$arguments")

            var key = it.getString("key")
            Log.i("TAG", "key=$key")

            var id = it.getString("id")
            Log.i("TAG", "id=$id")

        }
}

Also add the nav-graph on AndroidManifest.xml in related Activity :

 <activity
        android:name=".MainActivity"
        android:theme="@style/AppTheme.NoActionBar" >

    <nav-graph android:value="@navigation/main_navigation"/>

</activity>
like image 77
Hamed Jaliliani Avatar answered Sep 22 '22 02:09

Hamed Jaliliani