I have an app with a service and ongoing notification and two activities:
Activity A
and Activity B
.
Activity A
has a button that starts Activity B
.
When clicking the notification I want the next scenarios to happen:
- If app closed -> launch the app with
Activity A
. - If app is opened either in foreground or in the background (opened app list) in either activity -> show the current activity without launching a new
Activity A
, meaning if I am inActivity B
-> show currentActivity B
and same forActivity A
without launching a new one.
I searched a lot and finally found this answer, saying that you need to do:
final Intent notificationIntent = new Intent(context, ActivityA.class);
notificationIntent.setAction(Intent.ACTION_MAIN);
notificationIntent.addCategory(Intent.CATEGORY_LAUNCHER);
notificationIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
In the Manifest I have:
Notice I used android:launchMode="singleTop".
<activity
android:name=".ActivityA"
android:configChanges="keyboard|keyboardHidden|orientation|screenSize"
android:label="@string/app_name"
android:launchMode="singleTop"
android:theme="@style/AppTheme.NoActionBar"></activity>
<activity
android:name=".ActivityB"
android:configChanges="keyboard|keyboardHidden|orientation|screenSize"
android:theme="@style/AppTheme.NoActionBar"></activity>
It doesn't seem to work consistency enough, and it never works if to do the following: start app -> start activity A
-> start service (show notification) -> go to Activity B
-> click notification opens Activity A
.
I was wondering if there is a better consistent solution: