简体   繁体   English

带有BottomNavigationView 和多个片段的NavigationUI:禁用ActionBar 向上/后退按钮

[英]NavigationUI with BottomNavigationView and multiple fragments: disable ActionBar up/back button

I have a BottomNavigationView and a set of fragments which are the first level destinations for it.我有一个 BottomNavigationView 和一组片段,它们是它的第一级目的地。 I would like to disable the back button from the ActionBar for these fragments.我想为这些片段从 ActionBar 禁用后退按钮。 How can I achieve this?我怎样才能做到这一点?

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        navView = findViewById(R.id.nav_view);
        NavHostFragment navHostFragment = (NavHostFragment) getSupportFragmentManager().findFragmentById(R.id.nav_host_fragment);
        if (navHostFragment != null) {
            NavigationUI.setupWithNavController(navView, navHostFragment.getNavController());
            NavigationUI.setupActionBarWithNavController(this, navHostFragment.getNavController());
        }
    }

R.layout.activity_main does not explicitly include a Toolbar and I'm using the following theme: Theme.MaterialComponents.Light.DarkActionBar R.layout.activity_main没有明确包含工具栏,我使用以下主题: Theme.MaterialComponents.Light.DarkActionBar

In your setup (bottom navigation view + toolbar + fragment per bottom bar item) there should be no up/back button when implemented properly.在您的设置(底部导航视图 + 工具栏 + 每个底部栏项目的片段)中,正确实施时不应该有向上/后退按钮。 Multiple steps are required to implement, please refer to this https://github.com/android/architecture-components-samples/tree/master/NavigationAdvancedSample in your implementation - it basically has complete solution.实现需要多个步骤,请在你的实现中参考这个https://github.com/android/architecture-components-samples/tree/master/NavigationAdvancedSample - 它基本上有完整的解决方案。 Key point is to use https://github.com/android/architecture-components-samples/blob/master/NavigationAdvancedSample/app/src/main/java/com/example/android/navigationadvancedsample/NavigationExtensions.kt关键是使用https://github.com/android/architecture-components-samples/blob/master/NavigationAdvancedSample/app/src/main/java/com/example/android/navigationadvancedsample/NavigationExtensions.kt

PS.: I have to mention that system back button is working in this setup (and it should). PS.:我不得不提到系统后退按钮在这个设置中工作(它应该)。 It works like this: if you are on bottom bar's 2nd tab and user press system back, user will be navigated to bottom bar's 1st tab.它的工作原理是这样的:如果您在底部栏的第二个选项卡上并且用户按系统返回,用户将被导航到底部栏的第一个选项卡。 Only when user press system back on bottom bar's 1st tab, app will close.只有当用户在底部栏的第一个选项卡上按下系统时,应用程序才会关闭。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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