I am developing an android app which has 2 methodes of starting. One is the normal way by pressing the app icon on the phone. The other method is with a deep link from a website.
The deeplink also sends some data which the app needs to do some "stuff". However this should only be done once. When the deeplink activity is finished it start the main activity. However when I press back (on the device) and open the app from recents it opens the deeplink activity again.
I could exclude the deeplink activity from the recents in the manifest. This also excludes the mainactivity from the recent apps, this should not be the case.
How do I prevent the deeplink activity from beeing started from the recent apps?
My Manifest:
<activity
android:name="MainActivity"
android:label="@string/app_name"
android:screenOrientation="portrait"
android:launchMode="singleTask"
android:theme="@style/AppTheme">
</activity>
<activity
android:name="DeeplinkActivity"
android:label="@string/app_name"
android:noHistory="true"
android:screenOrientation="portrait"
android:launchMode="singleTask"
android:theme="@style/AppTheme">
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="app_name" android:host="test" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:host="test" android:path="/" android:scheme="app_name" />
</intent-filter>
</activity>
To switch to the MainActivity I do the following:
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
EDIT: This post was marked as duplicate with: Android: Starting app from 'recent applications' starts it with the last set of extras used in an intent
However that post is regarding the same activity. I want to change the root activity so when I start the app from recents it does not start the DeeplinkActivity. Yes as a workaround I could check the Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY flag. But then every time the user starts the app from recents the DeeplinkActivity is opened whilst it is not needed anymore.
Setting and or clearing aditional intent values does not seem to work. I use information from the getIntent().getData()
If you still feel as if this is a duplicate please explain.
Your problem is that both DeepLinkActivity
and MainActivity
are in the same task, so when the user selects the app from the list of recent tasks, Android will either bring an existing task to the foreground, or if there is no existing task (with live/active activities in it), start the Activity
that was the root Activity
in the most recent task. You can't predict which Activity
will be the root Activity
, since the task could be started with either DeepLinkActivity
or MainActivity
, depending on which one the user chose first.
You really need to have 2 separate tasks to do this. One task will have your DeepLinkActivity
in it, and this task should be excluded from the "recent task list". The other task will have your MainActivity
in it.
I assume that MainActivity
has the <intent-filter>
with ACTION=MAIN and CATEGORY=LAUNCHER, even though your posted manifest does not show this.
Your manifest should look like this:
<activity
android:name="MainActivity"
android:label="@string/app_name"
android:screenOrientation="portrait"
android:theme="@style/AppTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="DeeplinkActivity"
android:label="@string/app_name"
android:noHistory="true"
android:screenOrientation="portrait"
android:taskAffinity=""
android:excludeFromRecents="true"
android:theme="@style/AppTheme">
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="app_name" android:host="test" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:host="test" android:path="/" android:scheme="app_name" />
</intent-filter>
</activity>
You definitely do not need launchMode="singleTask"
for MainActivity
and you probably don't need it for DeepLinkActivity
either (that depends on what else you do with this).
Specifying taskAffinity=""
ensures that DeepLinkActivity
is not launched into the same task as MainActivity
, and allows you to launch MainActivity
from DeepLinkActivity
in a separate task. NOTE: Without specifying taskAffinity
, both activities will end up in the same task, even though you specify launchMode="singleTask"
for both of them.
Specifying excludeFromRecents="true"
on DeepLinkActivity
tells Android that the task with DeepLinkActivity
as its root activity should not appear in the list of recent tasks.
When you launch MainActivity
from DeepLinkActivity
, you should do this:
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
and make sure that you call finish()
on DeepLinkActivity
.
If necessary you can also add noHistory="true"
to DeepLinkActivity
, but I don't think it is necessary. If the user is in DeepLinkActivity
and gets an incoming phone call, after the phone call is ended, DeepLinkActivity
should be displayed. If you specify noHistory="true"
, DeepLinkActivity
would be finished immediately when the user accepts the incoming phone call.
Let me know if this is clear and works for you.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With