Skip to content Skip to sidebar Skip to footer

Set Different Activities For Pending Intent Of Notification

I am currently facing the problem of setting pending action for two different activities to notification. I have a ParentActivity and a ChildActivity. I want open ChildActivity on

Solution 1:

While there may be several ways to achieve this, following is the one I can think of.

First, you should get whether ChildActivity is active or not, through this link

Check whether activity is active

Store this in some variable childActive, then you can initialize different notificationIntents checking the value without using task TaskStackBuilder.

For example;

Intent notificationIntent = null;
if(childActive)
    notificationIntent = new Intent(context, ChildActivity.class);
else
    notificationIntent = new Intent(context, ParentActivity.class);

PendingIntent contentIntent = PendingIntent.getActivity(context,
                0, notificationIntent,
                PendingIntent.FLAG_CANCEL_CURRENT);

Solution 2:

Have your Notification launch a simple dispatch Activity. This Activity does the following in onCreate():

super.onCreate(...);
if (ChildActivity.running) {
    // ChildActivity is running, so redirect to itIntentchildIntent=newIntent(this, ChildActivity.class);
    // Add necessary flags, maybe FLAG_ACTIVITY_CLEAR_TOP, it depends what the rest of your app looks like
    childIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
    startActivity(childIntent);
} else {
    // Child is not running, so redirect to parentIntentparentIntent=newIntent(this, ParentIntent.class);
    // Add necessary flags, maybe FLAG_ACTIVITY_CLEAR_TOP, it depends what the rest of your app looks like
    parentIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
    startActivity(parentIntent);
}
finish();

In ChildActivity do this:

publicstaticboolean running; // Set when this Activity is active

In ChildActivity.onCreate() add this:

running = true;

In ChildActivity.onDestroy() add this:

running = false;

Post a Comment for "Set Different Activities For Pending Intent Of Notification"