简体   繁体   中英

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. I would like to disable the back button from the ActionBar for these fragments. 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

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. 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

PS.: I have to mention that system back button is working in this setup (and it should). 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.

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