Ofek Agmon Ofek Agmon - 6 months ago 32
Android Question

android - notification click shows current activity, if app is closed start new activity

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:


  1. If app closed -> launch the app with
    Activity A
    .

  2. 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 in
    Activity B
    -> show current
    Activity B
    and same for
    Activity 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:

Answer

Of course it would always open ActivityA because you defined it here :

final Intent notificationIntent = new Intent(context, ActivityA.class);

what you need to do is define where you were at the end. here is how i would handle this:

Make a class and extend it from Application class

define a variable here, it can be boolean, enum,int ,etc... when you enter any of those activities, change that parameter.

in OnDestroy of each activity reset that particular variable to its default value.

In your service before making the intent, check which state you were at last and make your intent based on that.

public class MY_APPLCIATION extends Application {
    public static Boolean ActivityAIsRunning=false;
    public static Boolean ActivityBIsRunning=false;
}

override onResume and onDestroy of both activities :

public class ActivityA extends AppCompatActivity {
    // ......
     @Override
protected void onDestroy() {
    super.onDestroy();
    MY_APPLCIATION.ActivityAIsRunning=false;
}

@Override
protected void onResume() {
    super.onResume();
    MY_APPLCIATION.ActivityAIsRunning=true;
}
}

ActivityB :

public class ActivityB extends AppCompatActivity {
    // ......
     @Override
protected void onDestroy() {
    super.onDestroy();
    MY_APPLCIATION.ActivityBIsRunning=false;
}

@Override
protected void onResume() {
    super.onResume();
    MY_APPLCIATION.ActivityBIsRunning=true;
}
}

now there is a trick which you should pay attention to :

You should define the intent when you are creating the notification !

you should have another activity ( lets called it MY_NOTIFICATION_ACTIVITY ). and the notification should always go to this activity. now in that activity check which boolean is set and then call the desired activity (ActivityA or ActivityB). Do not forget that in your manifest you should make ActivityB singleInstance too !!!

*** ANOTHER SIMPLE METHOD

another simple method is that you update the notification in OnResume of each activity. by entering each activity you change the intent to that desired activity and make the same notification and update the last notification.