Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Start Activity with backstack

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???

like image 642
AndroidDev Avatar asked Jul 13 '16 21:07

AndroidDev


People also ask

What is task Backstack?

A task is a collection of activities that users interact with when trying to do something in your app. These activities are arranged in a stack—the back stack—in the order in which each activity is opened. For example, an email app might have one activity to show a list of new messages.

What code would you use to go back to a previous activity?

Android activities are stored in the activity stack. Going back to a previous activity could mean two things. You opened the new activity from another activity with startActivityForResult. In that case you can just call the finishActivity() function from your code and it'll take you back to the previous activity.

What does finish () do in Android?

On Clicking the back button from the New Activity, the finish() method is called and the activity destroys and returns to the home screen.


1 Answers

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();
like image 102
njzk2 Avatar answered Oct 27 '22 10:10

njzk2