Max Bublikoff
Max Bublikoff

Reputation: 1234

Lifecycle and Activity Stack

In the application activities are stacked like this: A - > B - > C - > D - > E. If I receive a particular notification and click on it, Activity E is started. If I then click back (button on phone or button on actionbar), the application exit.

How do I make the transition to Activity D in this case, and then back through C, B, and A?

My code of back button:

public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case android.R.id.home:
        finish();
        break;
    }
    return true;
}

Everything is okay when starting the application normally. The problem is when Activity starts from the notification.

Upvotes: 3

Views: 235

Answers (2)

Chilledrat
Chilledrat

Reputation: 2605

Android has the functionality you're after built in, and it is already well documented. To begin with you should look at the TaskStackBuilder class. It was introduced in JellyBean, but is already included in the support library, and you use it to build a synthetic TaskStack which is what you need. A summary from the documentation reads:

When crossing from one task stack to another post-Android 3.0, the application should synthesize a back stack/history for the new task so that the user may navigate out of the new task and back to the Launcher by repeated presses of the back key. Back key presses should not navigate across task stacks.

TaskStackBuilder provides a way to obey the correct conventions around cross-task navigation.

How you build it is going to depend on the relationships of the Activities in your app, but the Tasks and Back Stack developers guide is a good read to help you decide, as is the Navigating with Up and Back design guide, if this is all new to you.

You'll find some code examples in the Implementing Effective Navigation lessons, also on the Android developers site, in the training section.

Incidentally, the button on the ActionBar is referred to as Up. Even though it sometimes shares the same functionality as the back button, the two are not the same (I assume that's the one you are talking about ;-) .)

Upvotes: 3

Steve
Steve

Reputation: 33

I think you can solve your problem by sending an intent from Activity E to Activity D, and so on. Therefore you should overwrite the method

onBackPressed()

that is called when you click on the back button.

Upvotes: 1

Related Questions