Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sample Notifications - Incorrectly uses TaskStackBuilder based on the "Start an Activity from a Notification" guidelines #336

Open
jkasten2 opened this issue Apr 6, 2022 · 0 comments

Comments

@jkasten2
Copy link

jkasten2 commented Apr 6, 2022

Issue

One Line Summary

The Notification sample project in this repo does NOT correctly use TaskStackBuilder based on the official Android "Start an Activity from a Notification" docs.

Details

According to the code comments in the MainActivity.java the "MESSAGING_STYLE" and "BIG_PICTURE_STYLE" (however it seems the intent was "INBOX_STYLE" should be too) should be following the "Regular activity" outlined in "Build a PendingIntent with a back stack" guide however it makes two mistakes.

  1. PendingIntent not generated from stackBuilder.getPendingIntent
    • This incorrect usage creates a bug. If the user swipes a way all tasks for the app and then the notification is opened the home screen will show instead when the user press the back button instead of the MainActivity for the app.
  2. addNextIntentWithParentStack should be used instead of addParentStack followed by addNextIntent
    • While both work as indented it would be best to show the same example code as the guide, unless there is a specific case it showing in the example.

Fix

There are 3 spots where this is wrong but the fix for inbox example:

Change From:

        // Adds the back stack.
        stackBuilder.addParentStack(InboxMainActivity.class);
        // Adds the Intent to the top of the stack.
        stackBuilder.addNextIntent(mainIntent);
        // Gets a PendingIntent containing the entire back stack.
        PendingIntent mainPendingIntent =
                PendingIntent.getActivity(
                        this,
                        0,
                        mainIntent,
                        PendingIntent.FLAG_UPDATE_CURRENT
                );

Change To:

        // Add the intent, which inflates the back stack
        stackBuilder.addNextIntentWithParentStack(mainIntent);
        // Gets a PendingIntent containing the entire back stack.
        PendingIntent mainPendingIntent =
            stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant