Handle Toolbar back button with Navigation component
Asked Answered
I

4

17

I'm following single activity approach. I have navigation toolbar, whenever i go to other screens (fragments) instead of hamburger icon i will have back arrow.

What i want to achieve is, pop my current fragment using action on pressing toolbar back arrow.

I've tried

requireActivity().getOnBackPressedDispatcher().addCallback(this, new OnBackPressedCallback(true) {
    @Override
    public void handleOnBackPressed() {
        NavHostFragment.findNavController(EventDetailsFragment.this)
        .navigate(R.id.action_nav_event_details_to_nav_home);
    }
});

But not getting the call over there, i checked by running app in debug mode.

Improbable answered 23/10, 2019 at 6:30 Comment(1)
Any solution of this?Clubby
S
25

in Activity oncreate:

navController = findNavController(R.id.my_nav_host)
//my_nav_host defined in activity xml file as id of fragment or FragmentContainerView
val appBarConfiguration = AppBarConfiguration(navController.graph)
NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration)

and:

override fun onOptionsItemSelected(item: MenuItem): Boolean {
    if (item.itemId == android.R.id.home) {
        onBackPressed()
        return true
    }
    return true
}

then in your fragment:

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)

    val callback: OnBackPressedCallback =
        object : OnBackPressedCallback(true /* enabled by default */) {
            override fun handleOnBackPressed() {
                //do what you want here
            }
        }
    requireActivity().onBackPressedDispatcher.addCallback(this, callback)
}
Sanborn answered 22/5, 2020 at 5:53 Comment(1)
how i will get navController access in parent activity ?Hambley
Z
1

Add this code in parent activity

Add in onCreate method

getSupportActionBar().setDisplayHomeAsUpEnabled(true);

Add this method also in parent activity

 @Override
public boolean onSupportNavigateUp() {
    return super.onSupportNavigateUp();
}
Zaremski answered 23/10, 2019 at 6:46 Comment(3)
Hi, I'm getting the callback here on pressing Toolbar back button. How do i check my current Fragment or Destination Fragment from here.Improbable
in onbackpress-> Fragment oldFragment = getFragmentManager().findFragmentByTag("ADD_NEW_ANNOUNCEMENT"; if (oldFragment != null) { getFragmentManager().beginTransaction().remove(oldFragment).commit(); }Zaremski
I've using Navigation Component(Navigation Graph). I don't need to do Fragment oldFragment = getFragmentManager().findFragmentByTag("ADD_NEW_ANNOUNCEMENT"); any of these while using that.Improbable
B
1

If you are using custom toolbar in your xml, you may consider using below approach

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

    setSupportActionBar(activity_chat_toolbar)

       
   activity_chat_toolbar.setNavigationOnClickListener {
        onBackPressed() <-- custom toolbar's back press
    }

    val navHostFrag =
            supportFragmentManager.findFragmentById(R.id.chat_fragment_container_view) as NavHostFragment
    navController = navHostFrag.navController
    navController.setGraph(R.navigation.chat_nav_graph, intent.extras)

    setupActionBarWithNavController(navController)
}

// Function to check startDestination to finish() the parent activity
override fun onBackPressed() {
    if(navController.graph.startDestination == navController.currentDestination?.id) {
        finish()
    } else {
        super.onBackPressed()
    }
}

override fun onSupportNavigateUp(): Boolean {
   return navController.navigateUp() || super.onSupportNavigateUp()
}
Bolten answered 13/3, 2021 at 8:21 Comment(0)
B
-2
Toolbar toolbar = findviewbyid(R.id.toolbar);
toolbar.setnavigationonclicklistener(new view.onclicklistener(){
 code here  
});
setsupportactionbar(toolbar);
Brayer answered 23/10, 2019 at 6:54 Comment(1)
Hi, I'm getting the callback here whenever i press toolbar icons even for hamburger icon.Improbable

© 2022 - 2024 — McMap. All rights reserved.