Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ActionBar Up button and Navigation pattern

I want to implement Navigation Pattern in my app with Up button in ActionBar.

I have Details Activity, here I can come from home, favorites and search screen. Also I can open this screen from browser(handling specific url). When user press Up button, I use flush() method, to emulate back navigation. But for case, when user come from browser, I want to open home screen instead of previous browser activity. How I can recognize, that previous activity was from another app, and navigate to home screen?

like image 673
barbarian Avatar asked May 25 '12 11:05

barbarian


1 Answers

Up Should always navigate to the hierarchical parent of the activity and Back should always navigate temporally.

In other words you should leave Back as it is.

As for Up, it should always go to the same place no matter where it came from. So if you normally come to the DetailsActivity from YourListActivity, Up should always go there no matter where you came from. What is the most likely place is up to your discretion, but it should always be the same.

If you come to the Details Activity from a non-normal location (such as the browser, another activity, widget, or notification) you should recreate your task stack so navigation using up results in the same path. Here is an example from the Android Developer Training:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case android.R.id.home:
        Intent upIntent = new Intent(this, YourListActivity.class);
        if (NavUtils.shouldUpRecreateTask(this, upIntent)) {
            // This activity is not part of the application's task, so
            // create a new task
            // with a synthesized back stack.
            TaskStackBuilder
                    .from(this)
                    .addNextIntent(new Intent(this, HomeActivity.class))
                    .addNextIntent(upIntent).startActivities();
            finish();
        } else {
            // This activity is part of the application's task, so simply
            // navigate up to the hierarchical parent activity.
            NavUtils.navigateUpTo(this, upIntent);
        }
        return true;
    }
}

Here is the Android Training on Implementing Navigation

(http://developer.android.com/training/implementing-navigation/index.html).

You will need the support library for NavUtils and TaskStackBuilder.

like image 114
Joseph Shanak Avatar answered Sep 30 '22 04:09

Joseph Shanak