ActionBar Up button and Navigation pattern

前端 未结 1 1668
北恋
北恋 2020-12-14 04:33

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

相关标签:
1条回答
  • 2020-12-14 05:08

    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.

    0 讨论(0)
提交回复
热议问题