Remove up button from action bar when navigating using BottomNavigationView with Android Navigation UI...












6















I've created a small app that has three fragments for top-level navigation through a BottomNavigationView. If you launch the app and click on a navigation button on the bottom nav, you are presented with an up button in the action bar. Here is the code for the activity:



class MainActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.main_activity)

setSupportActionBar(toolbar)

val navController = navHostFragment.findNavController()
setupActionBarWithNavController(this, navController)
setupWithNavController(bottomNav, navController)
}

override fun onSupportNavigateUp(): Boolean
= findNavController(navHostFragment).navigateUp()

}


Here is a screenshot of the result. The app is launched on the home screen and all I've done is simply click the profile button from the BottomNavigationView.



enter image description here



I've tried listening to the BottomNavigationView's item selections and navigating manually using different NavOptions to no avail. Is there anything we can do to avoid showing an up button in the action bar while the user is navigating with a BottomNavigationView?










share|improve this question





























    6















    I've created a small app that has three fragments for top-level navigation through a BottomNavigationView. If you launch the app and click on a navigation button on the bottom nav, you are presented with an up button in the action bar. Here is the code for the activity:



    class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.main_activity)

    setSupportActionBar(toolbar)

    val navController = navHostFragment.findNavController()
    setupActionBarWithNavController(this, navController)
    setupWithNavController(bottomNav, navController)
    }

    override fun onSupportNavigateUp(): Boolean
    = findNavController(navHostFragment).navigateUp()

    }


    Here is a screenshot of the result. The app is launched on the home screen and all I've done is simply click the profile button from the BottomNavigationView.



    enter image description here



    I've tried listening to the BottomNavigationView's item selections and navigating manually using different NavOptions to no avail. Is there anything we can do to avoid showing an up button in the action bar while the user is navigating with a BottomNavigationView?










    share|improve this question



























      6












      6








      6


      0






      I've created a small app that has three fragments for top-level navigation through a BottomNavigationView. If you launch the app and click on a navigation button on the bottom nav, you are presented with an up button in the action bar. Here is the code for the activity:



      class MainActivity : AppCompatActivity() {

      override fun onCreate(savedInstanceState: Bundle?) {
      super.onCreate(savedInstanceState)
      setContentView(R.layout.main_activity)

      setSupportActionBar(toolbar)

      val navController = navHostFragment.findNavController()
      setupActionBarWithNavController(this, navController)
      setupWithNavController(bottomNav, navController)
      }

      override fun onSupportNavigateUp(): Boolean
      = findNavController(navHostFragment).navigateUp()

      }


      Here is a screenshot of the result. The app is launched on the home screen and all I've done is simply click the profile button from the BottomNavigationView.



      enter image description here



      I've tried listening to the BottomNavigationView's item selections and navigating manually using different NavOptions to no avail. Is there anything we can do to avoid showing an up button in the action bar while the user is navigating with a BottomNavigationView?










      share|improve this question
















      I've created a small app that has three fragments for top-level navigation through a BottomNavigationView. If you launch the app and click on a navigation button on the bottom nav, you are presented with an up button in the action bar. Here is the code for the activity:



      class MainActivity : AppCompatActivity() {

      override fun onCreate(savedInstanceState: Bundle?) {
      super.onCreate(savedInstanceState)
      setContentView(R.layout.main_activity)

      setSupportActionBar(toolbar)

      val navController = navHostFragment.findNavController()
      setupActionBarWithNavController(this, navController)
      setupWithNavController(bottomNav, navController)
      }

      override fun onSupportNavigateUp(): Boolean
      = findNavController(navHostFragment).navigateUp()

      }


      Here is a screenshot of the result. The app is launched on the home screen and all I've done is simply click the profile button from the BottomNavigationView.



      enter image description here



      I've tried listening to the BottomNavigationView's item selections and navigating manually using different NavOptions to no avail. Is there anything we can do to avoid showing an up button in the action bar while the user is navigating with a BottomNavigationView?







      android-support-library android-navigation android-jetpack






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Sep 26 '18 at 1:10









      jclemus91

      1084




      1084










      asked May 12 '18 at 0:31









      jeffmcndjeffmcnd

      17311




      17311
























          3 Answers
          3






          active

          oldest

          votes


















          5














          Starting with 1.0.0-alpha07 you can use AppBarConfiguration to configure that behaviour.



          AppBarConfiguration has a Builder constructor so you can reate a new Builder with a specific set of top level destinations, referenced by their id (this id is the one you set on your navigation layout).



          Create new AppBarConfiguration:



          val appBarConfiguration = AppBarConfiguration
          .Builder(
          R.id.navigationHomeFragment,
          R.id.navigationListFragment,
          R.id.navigationProfileFragment)
          .build()


          Then, instead of setupActionBarWithNavController(this, navController) you need to call setupActionBarWithNavController(this, navController, appBarConfiguration)



          This is the right way to handle top navigation behaviours.






          share|improve this answer

































            3














            Use this instead of setupWithNavController:



            navController.addOnNavigatedListener(new NavController.OnNavigatedListener() {
            @Override
            public void onNavigated(@NonNull NavController controller, @NonNull NavDestination destination) {
            toolbar.setTitle(destination.getLabel());
            }
            });


            Or the equivalent in Kotlin.



            The only thing setupWithNavController does is add a listener to change the toolbar title, and creates the up button. If you don't want the up button, just add a listener which changes the toolbar title only.






            share|improve this answer
























            • Thanks Bob, this solved my problem. People like you are why I love Stackoverflow.

              – szaske
              Nov 6 '18 at 0:19



















            3














            I had the same problem and I found a way to get the current fragment with the NavController.addOnNavigatedListener, so I applied the following logic within my Activity and for now it works for me





            override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            setContentView(R.layout.activity_home)
            val navHost = supportFragmentManager
            .findFragmentById(R.id.navHostFragment) as NavHostFragment

            navHost.navController.addOnNavigatedListener { _, destination ->
            val showButton = showUpButton(destination.id)
            //Here occurs the magic
            supportActionBar?.setDisplayShowHomeEnabled(showButton)
            supportActionBar?.setDisplayHomeAsUpEnabled(showButton)
            }
            }

            //Check according to your convenience which fragment id
            //should show or hide the "Up Button"
            private fun showUpButton(id: Int): Boolean {
            return id != R.id.your_home_fragment && id != R.id.your_list_fragment
            && id != R.id.your_profile_fragment
            }


            And this is my project...



            enter image description here



            I do not know if it is the best option but if someone has any better suggestion, it will be welcome






            share|improve this answer

























              Your Answer






              StackExchange.ifUsing("editor", function () {
              StackExchange.using("externalEditor", function () {
              StackExchange.using("snippets", function () {
              StackExchange.snippets.init();
              });
              });
              }, "code-snippets");

              StackExchange.ready(function() {
              var channelOptions = {
              tags: "".split(" "),
              id: "1"
              };
              initTagRenderer("".split(" "), "".split(" "), channelOptions);

              StackExchange.using("externalEditor", function() {
              // Have to fire editor after snippets, if snippets enabled
              if (StackExchange.settings.snippets.snippetsEnabled) {
              StackExchange.using("snippets", function() {
              createEditor();
              });
              }
              else {
              createEditor();
              }
              });

              function createEditor() {
              StackExchange.prepareEditor({
              heartbeatType: 'answer',
              autoActivateHeartbeat: false,
              convertImagesToLinks: true,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: 10,
              bindNavPrevention: true,
              postfix: "",
              imageUploader: {
              brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
              contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
              allowUrls: true
              },
              onDemand: true,
              discardSelector: ".discard-answer"
              ,immediatelyShowMarkdownHelp:true
              });


              }
              });














              draft saved

              draft discarded


















              StackExchange.ready(
              function () {
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f50301820%2fremove-up-button-from-action-bar-when-navigating-using-bottomnavigationview-with%23new-answer', 'question_page');
              }
              );

              Post as a guest















              Required, but never shown

























              3 Answers
              3






              active

              oldest

              votes








              3 Answers
              3






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              5














              Starting with 1.0.0-alpha07 you can use AppBarConfiguration to configure that behaviour.



              AppBarConfiguration has a Builder constructor so you can reate a new Builder with a specific set of top level destinations, referenced by their id (this id is the one you set on your navigation layout).



              Create new AppBarConfiguration:



              val appBarConfiguration = AppBarConfiguration
              .Builder(
              R.id.navigationHomeFragment,
              R.id.navigationListFragment,
              R.id.navigationProfileFragment)
              .build()


              Then, instead of setupActionBarWithNavController(this, navController) you need to call setupActionBarWithNavController(this, navController, appBarConfiguration)



              This is the right way to handle top navigation behaviours.






              share|improve this answer






























                5














                Starting with 1.0.0-alpha07 you can use AppBarConfiguration to configure that behaviour.



                AppBarConfiguration has a Builder constructor so you can reate a new Builder with a specific set of top level destinations, referenced by their id (this id is the one you set on your navigation layout).



                Create new AppBarConfiguration:



                val appBarConfiguration = AppBarConfiguration
                .Builder(
                R.id.navigationHomeFragment,
                R.id.navigationListFragment,
                R.id.navigationProfileFragment)
                .build()


                Then, instead of setupActionBarWithNavController(this, navController) you need to call setupActionBarWithNavController(this, navController, appBarConfiguration)



                This is the right way to handle top navigation behaviours.






                share|improve this answer




























                  5












                  5








                  5







                  Starting with 1.0.0-alpha07 you can use AppBarConfiguration to configure that behaviour.



                  AppBarConfiguration has a Builder constructor so you can reate a new Builder with a specific set of top level destinations, referenced by their id (this id is the one you set on your navigation layout).



                  Create new AppBarConfiguration:



                  val appBarConfiguration = AppBarConfiguration
                  .Builder(
                  R.id.navigationHomeFragment,
                  R.id.navigationListFragment,
                  R.id.navigationProfileFragment)
                  .build()


                  Then, instead of setupActionBarWithNavController(this, navController) you need to call setupActionBarWithNavController(this, navController, appBarConfiguration)



                  This is the right way to handle top navigation behaviours.






                  share|improve this answer















                  Starting with 1.0.0-alpha07 you can use AppBarConfiguration to configure that behaviour.



                  AppBarConfiguration has a Builder constructor so you can reate a new Builder with a specific set of top level destinations, referenced by their id (this id is the one you set on your navigation layout).



                  Create new AppBarConfiguration:



                  val appBarConfiguration = AppBarConfiguration
                  .Builder(
                  R.id.navigationHomeFragment,
                  R.id.navigationListFragment,
                  R.id.navigationProfileFragment)
                  .build()


                  Then, instead of setupActionBarWithNavController(this, navController) you need to call setupActionBarWithNavController(this, navController, appBarConfiguration)



                  This is the right way to handle top navigation behaviours.







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Nov 25 '18 at 13:21

























                  answered Nov 25 '18 at 1:11









                  Javier CancioJavier Cancio

                  583413




                  583413

























                      3














                      Use this instead of setupWithNavController:



                      navController.addOnNavigatedListener(new NavController.OnNavigatedListener() {
                      @Override
                      public void onNavigated(@NonNull NavController controller, @NonNull NavDestination destination) {
                      toolbar.setTitle(destination.getLabel());
                      }
                      });


                      Or the equivalent in Kotlin.



                      The only thing setupWithNavController does is add a listener to change the toolbar title, and creates the up button. If you don't want the up button, just add a listener which changes the toolbar title only.






                      share|improve this answer
























                      • Thanks Bob, this solved my problem. People like you are why I love Stackoverflow.

                        – szaske
                        Nov 6 '18 at 0:19
















                      3














                      Use this instead of setupWithNavController:



                      navController.addOnNavigatedListener(new NavController.OnNavigatedListener() {
                      @Override
                      public void onNavigated(@NonNull NavController controller, @NonNull NavDestination destination) {
                      toolbar.setTitle(destination.getLabel());
                      }
                      });


                      Or the equivalent in Kotlin.



                      The only thing setupWithNavController does is add a listener to change the toolbar title, and creates the up button. If you don't want the up button, just add a listener which changes the toolbar title only.






                      share|improve this answer
























                      • Thanks Bob, this solved my problem. People like you are why I love Stackoverflow.

                        – szaske
                        Nov 6 '18 at 0:19














                      3












                      3








                      3







                      Use this instead of setupWithNavController:



                      navController.addOnNavigatedListener(new NavController.OnNavigatedListener() {
                      @Override
                      public void onNavigated(@NonNull NavController controller, @NonNull NavDestination destination) {
                      toolbar.setTitle(destination.getLabel());
                      }
                      });


                      Or the equivalent in Kotlin.



                      The only thing setupWithNavController does is add a listener to change the toolbar title, and creates the up button. If you don't want the up button, just add a listener which changes the toolbar title only.






                      share|improve this answer













                      Use this instead of setupWithNavController:



                      navController.addOnNavigatedListener(new NavController.OnNavigatedListener() {
                      @Override
                      public void onNavigated(@NonNull NavController controller, @NonNull NavDestination destination) {
                      toolbar.setTitle(destination.getLabel());
                      }
                      });


                      Or the equivalent in Kotlin.



                      The only thing setupWithNavController does is add a listener to change the toolbar title, and creates the up button. If you don't want the up button, just add a listener which changes the toolbar title only.







                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Sep 20 '18 at 18:43









                      Bob bobbingtonBob bobbington

                      912910




                      912910













                      • Thanks Bob, this solved my problem. People like you are why I love Stackoverflow.

                        – szaske
                        Nov 6 '18 at 0:19



















                      • Thanks Bob, this solved my problem. People like you are why I love Stackoverflow.

                        – szaske
                        Nov 6 '18 at 0:19

















                      Thanks Bob, this solved my problem. People like you are why I love Stackoverflow.

                      – szaske
                      Nov 6 '18 at 0:19





                      Thanks Bob, this solved my problem. People like you are why I love Stackoverflow.

                      – szaske
                      Nov 6 '18 at 0:19











                      3














                      I had the same problem and I found a way to get the current fragment with the NavController.addOnNavigatedListener, so I applied the following logic within my Activity and for now it works for me





                      override fun onCreate(savedInstanceState: Bundle?) {
                      super.onCreate(savedInstanceState)
                      setContentView(R.layout.activity_home)
                      val navHost = supportFragmentManager
                      .findFragmentById(R.id.navHostFragment) as NavHostFragment

                      navHost.navController.addOnNavigatedListener { _, destination ->
                      val showButton = showUpButton(destination.id)
                      //Here occurs the magic
                      supportActionBar?.setDisplayShowHomeEnabled(showButton)
                      supportActionBar?.setDisplayHomeAsUpEnabled(showButton)
                      }
                      }

                      //Check according to your convenience which fragment id
                      //should show or hide the "Up Button"
                      private fun showUpButton(id: Int): Boolean {
                      return id != R.id.your_home_fragment && id != R.id.your_list_fragment
                      && id != R.id.your_profile_fragment
                      }


                      And this is my project...



                      enter image description here



                      I do not know if it is the best option but if someone has any better suggestion, it will be welcome






                      share|improve this answer






























                        3














                        I had the same problem and I found a way to get the current fragment with the NavController.addOnNavigatedListener, so I applied the following logic within my Activity and for now it works for me





                        override fun onCreate(savedInstanceState: Bundle?) {
                        super.onCreate(savedInstanceState)
                        setContentView(R.layout.activity_home)
                        val navHost = supportFragmentManager
                        .findFragmentById(R.id.navHostFragment) as NavHostFragment

                        navHost.navController.addOnNavigatedListener { _, destination ->
                        val showButton = showUpButton(destination.id)
                        //Here occurs the magic
                        supportActionBar?.setDisplayShowHomeEnabled(showButton)
                        supportActionBar?.setDisplayHomeAsUpEnabled(showButton)
                        }
                        }

                        //Check according to your convenience which fragment id
                        //should show or hide the "Up Button"
                        private fun showUpButton(id: Int): Boolean {
                        return id != R.id.your_home_fragment && id != R.id.your_list_fragment
                        && id != R.id.your_profile_fragment
                        }


                        And this is my project...



                        enter image description here



                        I do not know if it is the best option but if someone has any better suggestion, it will be welcome






                        share|improve this answer




























                          3












                          3








                          3







                          I had the same problem and I found a way to get the current fragment with the NavController.addOnNavigatedListener, so I applied the following logic within my Activity and for now it works for me





                          override fun onCreate(savedInstanceState: Bundle?) {
                          super.onCreate(savedInstanceState)
                          setContentView(R.layout.activity_home)
                          val navHost = supportFragmentManager
                          .findFragmentById(R.id.navHostFragment) as NavHostFragment

                          navHost.navController.addOnNavigatedListener { _, destination ->
                          val showButton = showUpButton(destination.id)
                          //Here occurs the magic
                          supportActionBar?.setDisplayShowHomeEnabled(showButton)
                          supportActionBar?.setDisplayHomeAsUpEnabled(showButton)
                          }
                          }

                          //Check according to your convenience which fragment id
                          //should show or hide the "Up Button"
                          private fun showUpButton(id: Int): Boolean {
                          return id != R.id.your_home_fragment && id != R.id.your_list_fragment
                          && id != R.id.your_profile_fragment
                          }


                          And this is my project...



                          enter image description here



                          I do not know if it is the best option but if someone has any better suggestion, it will be welcome






                          share|improve this answer















                          I had the same problem and I found a way to get the current fragment with the NavController.addOnNavigatedListener, so I applied the following logic within my Activity and for now it works for me





                          override fun onCreate(savedInstanceState: Bundle?) {
                          super.onCreate(savedInstanceState)
                          setContentView(R.layout.activity_home)
                          val navHost = supportFragmentManager
                          .findFragmentById(R.id.navHostFragment) as NavHostFragment

                          navHost.navController.addOnNavigatedListener { _, destination ->
                          val showButton = showUpButton(destination.id)
                          //Here occurs the magic
                          supportActionBar?.setDisplayShowHomeEnabled(showButton)
                          supportActionBar?.setDisplayHomeAsUpEnabled(showButton)
                          }
                          }

                          //Check according to your convenience which fragment id
                          //should show or hide the "Up Button"
                          private fun showUpButton(id: Int): Boolean {
                          return id != R.id.your_home_fragment && id != R.id.your_list_fragment
                          && id != R.id.your_profile_fragment
                          }


                          And this is my project...



                          enter image description here



                          I do not know if it is the best option but if someone has any better suggestion, it will be welcome







                          share|improve this answer














                          share|improve this answer



                          share|improve this answer








                          edited Oct 26 '18 at 19:54

























                          answered May 18 '18 at 21:07









                          jclemus91jclemus91

                          1084




                          1084






























                              draft saved

                              draft discarded




















































                              Thanks for contributing an answer to Stack Overflow!


                              • Please be sure to answer the question. Provide details and share your research!

                              But avoid



                              • Asking for help, clarification, or responding to other answers.

                              • Making statements based on opinion; back them up with references or personal experience.


                              To learn more, see our tips on writing great answers.




                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function () {
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f50301820%2fremove-up-button-from-action-bar-when-navigating-using-bottomnavigationview-with%23new-answer', 'question_page');
                              }
                              );

                              Post as a guest















                              Required, but never shown





















































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown

































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown







                              Popular posts from this blog

                              A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks

                              Calculate evaluation metrics using cross_val_predict sklearn

                              Insert data from modal to MySQL (multiple modal on website)