简体   繁体   中英

Getting null value from intent in deep link

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?

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
}

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM