简体   繁体   English

如何获得完全像按下后退按钮一样的导航行为

[英]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.

相关问题 Jetpack 导航组件上具有不同行为的后退和向上按钮 - Back and Up Button with different behavior on Jetpack Navigation Component 在 Flutter 中按下后退按钮时关闭导航抽屉 - Close Navigation Drawer on Back Button Pressed in Flutter 使用导航组件时如何覆盖工具栏中后退按钮的行为 - How to override the behavior of back button in toolbar when using navigation component 如何在按下返回按钮的情况下从一个导航活动返回另一个导航活动? - How to go back from one to another Navigation activity on back button pressed? React Native Router Flux-如何在Android中访问从导航栏(而非硬件后退按钮)按下的后退? - React native router flux - How to access back pressed from navigation bar (not hardware back button) in android? 按下后退按钮时模拟 - simulate up when back button is pressed 向上按钮与向后按钮有何不同? 以及如何使Up Button像Back Button一样工作? - What differs Up Button to Back Button? And how to make Up Button work like Back Button? 按下后退按钮时,如何还原ListView并获取列表内容? - How to restore the ListView and get Contents of List when back button in pressed? 带有后退按钮的片段导航具有不良行为 - Fragment navigation with back button undesired behavior 如何在按下后完美处理底部导航 - How to handle bottom navigation perfectly with back pressed
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM