Getting null value from intent in deep link
Asked Answered
C

2

8

I have added a deep link to my app which maps an activity to a particular web page on my website (Link referred: https://developer.android.com/training/app-links/deep-linking ). While handling the deep link in my Java code, getAction() and getData() methods give me null value. I tried testing it here: https://firebase.google.com/docs/app-indexing/android/test (This gave me perfect result) But the same link opens in A web browser rather than in my app when clicked.

Android Manifest code :

<activity
    android:name=".MainActivity"
    android:screenOrientation="portrait"
    android:launchMode="singleTask"
    android:exported="true">

    <tools:validation testUrl="https://www.mywebsite.com/xyz" />
    <intent-filter android:autoVerify="true">
        <action android:name="android.intent.action.VIEW" />
        <category android:name="android.intent.category.DEFAULT" />
        <category android:name="android.intent.category.BROWSABLE" />
        <data
            android:scheme="https"
            android:host="www.mywebsite.com"
            android:pathPrefix="/xyz" />
    </intent-filter>
</activity>

Java Code :

    Intent intent = getIntent();
    String action = intent.getAction(); // getting null value here
    Uri data = intent.getData(); // getting null value here

I want that if the app is present, then it should be opened when the link is clicked or else the link will open the web page. Where and what am I missing?

Copycat answered 7/2, 2019 at 8:48 Comment(5)
I am also facing the same issue while implementing deep links in my Android app.Rasberry
I am using deep links provided by google in my android app. I have mapped an activity of my android app to a web page of my website.I wanted to know that if we click on the link of that particular page,it should redirect it to my app and open that specific activity if the app is present or else open the web page in the browser.if it is possible then please assist me with its solution.Thank you!Copycat
@AnirudhMishra, do you find solution?Sexlimited
Have you tried close/kill your application and then run test? Because possible issue is you already have intent from application start. In that case you have to check for new intent using override onNewIntent function.Lapse
@TomasIvan, thank you - you are totally right. Added plus to your answer:)Sexlimited
L
15

You can run into this problem because using singleTask. In that case you should use onNewIntent to 'update' intent, because onCreate and onResume will not handle it.

This is called for activities that set launchMode to "singleTop" in their package, or if a client used the Intent#FLAG_ACTIVITY_SINGLE_TOP flag when calling startActivity(Intent). In either case, when the activity is re-launched while at the top of the activity stack instead of a new instance of the activity being started, onNewIntent() will be called on the existing instance with the Intent that was used to re-launch it.

override fun onNewIntent(intent: Intent?) {
    super.onNewIntent(intent)

    //TODO: do something with new intent
}
Lapse answered 10/4, 2019 at 10:30 Comment(0)
L
3

instead of getting intent data in onCreate, get in onResume

   @Override
protected void onResume() {
    super.onResume();

    Intent intent = getIntent();
    if (intent != null && intent.getData() != null ){
        Toast.makeText(this, intent.getData().toString(), Toast.LENGTH_SHORT).show();
        webView.loadUrl(intent.getData().toString());
    }
}

add these lines of code which activity is attached for deep linking to open your website page

and mark it as a answer if it helped you to solve your problem

Lyudmila answered 7/2, 2019 at 9:52 Comment(2)
The link already opens the web page in a web browser, i want the link to open the MainActivity of my app.Thanks for your help!Copycat
it'not working - intent with data still null. I was trying to use this on Start and onResume, still with no luckSexlimited

© 2022 - 2024 — McMap. All rights reserved.