s-hunter
s-hunter

Reputation: 25826

Back button for single activity with multiple fragments

With jetpack navigation, single activity with multiple fragments. After navigating from the main fragment to another fragment via the action defined in the navigation graph, the hamburger menu icon stays the same, it did not change to a back arrow button.

How to change this hamburger menu icon to a back arrow button? when clicked, it should go back to the main fragment.

Creating a new project in Android Studio and choose Navigation Drawer Activity as the template will set up the single activity with 3 fragments described above.

Upvotes: 1

Views: 1215

Answers (3)

s-hunter
s-hunter

Reputation: 25826

I had it set up with multiple fragments in the AppBarConfiguration.

appBarConfiguration = AppBarConfiguration(
    setOf(
        R.id.nav_main, R.id.nav_detail
    ), drawerLayout
)
setupActionBarWithNavController(navController, appBarConfiguration)
navView.setupWithNavController(navController) 

Since the detail fragment was set up in the AppBarConfiguration, that's why when navigated to the detail fragment from the main fragment, the hamburger menu icon stayed the same because the detail fragment was set up to be one of the drawerlayout.

After removing the R.id.nav_detail from the AppBarConfiguration, then navigate to the detail fragment, the hamburger menu icon will automatically changes to a back arrow icon with the DrawerArrowDrawable animation.

appBarConfiguration = AppBarConfiguration(
    setOf(
        R.id.nav_main
    ), drawerLayout
)
setupActionBarWithNavController(navController, appBarConfiguration)
navView.setupWithNavController(navController) 

Upvotes: 1

Vijay S
Vijay S

Reputation: 418

Try this in your activity

NavigationView nav_view = (NavigationView) findViewById(R.id.nav_view);
          drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        NavHostFragment navHostFragment = (NavHostFragment) getSupportFragmentManager().findFragmentById(R.id.nav_host_fragment);

        appBarConfiguration =
                new AppBarConfiguration.Builder(navController.getGraph()).build();
        setupActionBarWithNavController(this, navController,appBarConfiguration);
        NavigationUI.setupWithNavController(nav_view, navController);
        NavigationUI.setupActionBarWithNavController(this, navController);
        NavigationUI.setupActionBarWithNavController(this, navController, drawer);

Upvotes: 0

Aman Shekhar
Aman Shekhar

Reputation: 2780

Just add this in your root activity.

if (supportFragmentManager.backStackEntryCount > 0) {
  supportActionBar!!.setDisplayHomeAsUpEnabled(true)
  toolbar.setNavigationOnClickListener {
      if (supportFragmentManager.backStackEntryCount > 0) {
          super.onBackPressed()
      } else {
          supportActionBar!!.setDisplayHomeAsUpEnabled(false)
          drawerLayout.addDrawerListener(toggle)
          toggle.syncState()
          drawerLayout.openDrawer(GravityCompat.START)
      }
  }
} else {
  supportActionBar!!.setDisplayHomeAsUpEnabled(false)
  drawerLayout.addDrawerListener(toggle)
  toggle.syncState()
}

Upvotes: 1

Related Questions