7

I'm trying to start an activity and at the same time correctly maintain a backstack to allow the user to use the back button. To do so, I'm following Google's instructions, but am getting nowhere. When I click my button, nothing happens (except the log output). What do I need to do to make this launch the next activity?

        mBtnNext.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                Log.d(TAG, "click");

                Intent intent = new Intent(ProfileSelectActivity.this, PermissionsRequestActivity.class);

                PendingIntent pendingIntent =
                        TaskStackBuilder.create(ProfileSelectActivity.this)
                                // add all of DetailsActivity's parents to the stack,
                                // followed by DetailsActivity itself
                                .addNextIntentWithParentStack(intent)
                                .getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);

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

It seems to me that there is a missing line at the end. I've tried startActivities();, startActivity(); among other things, but I'm unsuccessful with all of my attempts.

Thanks for any help.

EDIT

Attempting to implement the suggestion below from njzk2 but I'm still confused. In order to add startActivities() to the end of the TaskStackBuilder line, I have to remove any assignment to PendingIntent. This is probably OK. However, after the next activity starts, if I hit the back button I just get a white screen.

TaskStackBuilder.create(ProfileSelectActivity.this)
    .addNextIntentWithParentStack(intent)
    .startActivities();

I also noticed that my onResume method in the parent activity is not called when returning via the back button, so this could just be a problem of restoring state???

AndroidDev
  • 20,466
  • 42
  • 148
  • 239
  • There is conflicting information here. You say you're trying to implement a **back stack** but then provide code for an **actual button**. So which is it? You want a back stack which is what's triggered when users press the back (hard or soft) button on their phone? Or you're trying to implement some action when users press a button in your app? – iheanyi Jul 13 '16 at 22:08
  • According to the documentation, you don't need to do anything for this case. If you launch activity B from a button in activity A, pressing back will automatically take you back to activity A. – iheanyi Jul 13 '16 at 22:11
  • That's what I think, but I just get a white screen when I hit back, so I'm trying more explicit measures. – AndroidDev Jul 13 '16 at 22:12
  • 1
    Read this: https://developer.android.com/guide/components/tasks-and-back-stack.html Then go back to two empty activities with different text or buttons on screen. I suspect you're doing something wrong in either launching new activities or when your activity starts. You're not going to fix it by smashing the hammer of forcing something onto the back-stack if you're already screwing up the stack. – iheanyi Jul 13 '16 at 22:24

1 Answers1

17

What you are doing is creating (not displaying) a notification. (as the notification builder suggests.)

What you need to do is, as indicated in the doc, to call startActivities():

TaskStackBuilder.create(ProfileSelectActivity.this)
        .addNextIntentWithParentStack(intent)
        .startActivities();
njzk2
  • 38,969
  • 7
  • 69
  • 107
  • Thank you. I'm still unsuccessful as now when the next activity starts, the back button just results in a white screen. – AndroidDev Jul 13 '16 at 21:59
  • and you are sure that the `PermissionsRequestActivity` works correctly? do you have any error, any log? – njzk2 Jul 14 '16 at 00:56
  • That was indeed the issue. See my 'answer' above. Thank you very much for your help. – AndroidDev Jul 14 '16 at 03:35