I use deep links in my app.
This is my activity in manifest
<activity
android:name=".ui.MainActivity"
android:exported="true"
android:launchMode="singleTask"
android:screenOrientation="portrait"
android:theme="@style/AppTheme.NoActionBar.Launcher">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<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:host="app.mydomain.com"
android:pathPrefix="/prefix"
android:scheme="https" />
</intent-filter>
</activity>
And this is how I get deep link
Uri uri = getIntent().getData();
After I'm done with handling deep link and some other logic in MainActivity
, I navigate user to other activity, and finish MainActivity
.
The problem is, in certain scenario, I got deep link delivered over and over again. This is scenario whet problem happens:
At moment I click link, my app is not in memory or closed by tap on system back button.
After link handled in my app, I close app with system back button.
Then I open app by clicking on it's preview (not launcher icon) in menu with all in memory apps (opens by clicking on one system button on my phone). And deep link is delivered again.
The problem is, when link delivered I can't identify, is it this particular problematic case scenario, and I should ignore the link, or is user actually clicked the link, and I should handle it.
Saving the link in static variable to check if is it the same link would be not good, cause user may click the same link again to review the same data.
The problem also persists with Firebase Dynamic Links.
My phone runs on Android 12 (api level 31), Samsung S21
Found a solution, thanks to this answer
I check if activity was launched from history using this method, and if true, then I ignore deep link
private boolean isActivityLaunchedFromHistory() {
return getIntent().getFlags() ==
(Intent.FLAG_ACTIVITY_NEW_TASK
| Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY);
}