简体   繁体   中英

How to get Up Navigation behavior exactly like back button pressed

How can I get the Up Navigation button to behave like the back button? When I press the back button all my previous activities remain as the were prior. When I press the up button my activities return to initial state. I tried hooking up onBackPressed(); to case R.id.home on my onOptionsItemSelected but it still behaves differently.

Visual: Back Button Behavior- https://www.flickr.com/photos/151974643@N07/35702817566/in/dateposted-public/ Up Button Behavior - https://www.flickr.com/photos/151974643@N07/35702812926/in/dateposted-public/ Any help would be appreciated. Thank you!

You can do it several ways, put this code in onCreate() method of your activity

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        YOUR_ACTIVITY.this.onBackPressed();
    }
});

Hope it helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM