[英]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();
我试图挂钩onBackPressed(); to case R.id.home on my onOptionsItemSelected but it still behaves differently.
以防我onOptionsItemSelected上的R.id.home出现问题,但是它的行为仍然有所不同。
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. 视觉:后退按钮行为-https : //www.flickr.com/photos/151974643@N07/35702817566/in/dateposted-public/向上按钮行为-https: //www.flickr.com/photos/151974643@N07/ 35702812926 / in / dateposted-public /任何帮助将不胜感激。 Thank you!
谢谢!
You can do it several ways, put this code in onCreate() method of your activity 您可以通过多种方式完成此操作,并将此代码放入活动的onCreate()方法中
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. 希望能帮助到你。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.