2

I am opening an activity from notification, which opens fine. However, I want to open it's parent activity while I click 'back button', currently it exits the application directly. I want it to navigate to HomeScreenActivity.

Here is manifest declaration -

<activity
        android:name="com.discover.activities.MyTrialsActivity"
        android:exported="true"
        android:parentActivityName="com.discover.activities.HomeScreenActivity"
        android:screenOrientation="portrait">
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value="com.discover.activities.HomeScreenActivity" />
    </activity>

Here is my code to generate notification -

public static PendingIntent getAction(Activity context, int actionId) {
    Intent intent;
    PendingIntent pendingIntent;

    intent = new Intent(context, MyTrialsActivity.class);

    //intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

    TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
    // Adds the back stack
    //stackBuilder.addParentStack(HomeScreenActivity.class);
    stackBuilder.addParentStack(HomeScreenActivity.class);
    // Adds the Intent to the top of the stack
    stackBuilder.addNextIntent(intent);
    // Gets a PendingIntent containing the entire back stack
    pendingIntent =
            stackBuilder.getPendingIntent(0 /*request code */, PendingIntent.FLAG_ONE_SHOT);

    /*pendingIntent = PendingIntent.getActivity(context, 0 *//* Request code *//*, intent,
            PendingIntent.FLAG_UPDATE_CURRENT*//*|PendingIntent.FLAG_ONE_SHOT*//*);*/
    return pendingIntent;
}

/**
 * Create and show a simple notification containing the message.
 *
 * @param message Message to show in notification
 */
public static void sendNotification(Context context, String message, int actionId) {
    PendingIntent pendingIntent = NotifUtils.getAction((Activity) context, actionId);

    Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context)
            .setSmallIcon(R.mipmap.ic_launcher)
            .setContentTitle("Title")
            .setContentText(message)
            .setAutoCancel(true)
            .setSound(defaultSoundUri)
            .setVibrate(new long[]{1000})
            .setContentIntent(pendingIntent);

    NotificationManager notificationManager =
            (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

    notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
}
Darpan
  • 5,623
  • 3
  • 48
  • 80
  • Open `ParentActivity` from notification rather opening `ChildActivity` and set a flag in an intent. In `ParentActivity`, check for this flag and start `ChildActivity`. This way you can easily navigate back to parent activity. – Faraz Mar 10 '16 at 12:17
  • I had this in mind, but doesn't it sound like a workaround, while android provides whole way to ship a backstack with pendingIntent? – Darpan Mar 10 '16 at 12:22

4 Answers4

2

I you have everything set up correctly and it's still not working it might be that you need to uninstall and reinstall the app. It seems like some changes to the manifest are not updated properly when you run the app!

TFennis
  • 1,393
  • 1
  • 14
  • 23
1

Solution -

I added my child activity as well in addParentStack(MyTrialActivity.class);
And it worked as expected. I thought adding addNextIntent() should be doing that already, though it did not work that way..

Darpan
  • 5,623
  • 3
  • 48
  • 80
0

I found the solution in android's documentation

// Intent for the activity to open when user selects the notification
Intent detailsIntent = new Intent(this, DetailsActivity.class);

// Use TaskStackBuilder to build the back stack and get the PendingIntent
PendingIntent pendingIntent =
        TaskStackBuilder.create(this)
                        // add all of DetailsActivity's parents to the stack,
                        // followed by DetailsActivity itself
                        .addNextIntentWithParentStack(upIntent)
                        .getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);

NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder.setContentIntent(pendingIntent);

And, Here is the link.

Also, see this answer for more references.

Community
  • 1
  • 1
Faraz
  • 2,144
  • 1
  • 18
  • 28
  • I have done exact same thing what you are suggesting here, so this one did not work. Order of `.addNextIntentWithParentStack(parent); .addNextIntentWithParentStack(childIntent)` When I reverse this order, my notification was opening parent activity and on back press it was opening child activity. Clearly, here is what I am doing something wrong, as reverse order works, but not the real one.. – Darpan Mar 10 '16 at 12:44
  • addNextIntentWithParentStack only works if you set the android:parentActivityName in the manifest – Simon Jun 18 '21 at 02:26
0

Try using startActivities(Context context, Intent[] intents),

Intent homeIntent = new Intent(context, HomeScreenActivity.class);
        Intent newIntent = new Intent(context, MyTrialsActivity.class);
        Intent[] intents = new Intent[]{homeIntent, newIntent};

        ContextCompat.startActivities(context, intents);

So we can start multiple activities at same time, so while pressing Back button it will go to Home Page instead of quiting the application.

Amit Gupta
  • 8,914
  • 1
  • 25
  • 33
  • Can it be done with PendingIntent as well? As I need to open these activities from a notification. – Darpan Mar 10 '16 at 13:01