I want to add custom up navigation from fragment using Navigation component
In my build.gradle(app)
I use androidx.appcompat:appcompat:1.1.0-alpha04
dependency to have access to onBackPressedDispatcher
from activity.
So I implemented OnBackPressedCallback
in my fragment and registered callback to dispatcher:
requireActivity().onBackPressedDispatcher.addCallback(this)
I expected that pressing navigate up in toolbar will call it, but it doesn't. Pressing device's back button calls it as expected.
Is there a similar way to add some callback in fragment on navigate up action?
UPDATE
overridden methods onOptionsItemSelected
and onSupportNavigateUp
doesn't invoked on pressing up button in toolbar
onSupportNavigateUp() This method is called whenever the user chooses to navigate Up within your application's activity hierarchy from the action bar. @Nullable ActionMode.
If you want to use the Navigation component with Compose, you have two options: Define a navigation graph with the Navigation component for fragments. Define a navigation graph with a NavHost in Compose using Compose destinations. This is possible only if all of the screens in the navigation graph are composables.
Implement custom back navigation. ComponentActivity , the base class for FragmentActivity and AppCompatActivity , allows you to control the behavior of the Back button by using its OnBackPressedDispatcher , which you can retrieve by calling getOnBackPressedDispatcher() . Note: If your app uses Activity 1.5.
Create action bar variable and call function getSupportActionBar() in the java/kotlin file. Show back button using actionBar. setDisplayHomeAsUpEnabled(true) this will enable the back button.
I found a solution
handleOnBackPressed()
method invokes only on device's back button click. I wonder, why neither onOptionsItemSelected()
nor onSupportNavigateUp()
methods haven't been called on pressing "up button" in toolbar. And the answer is I used
NavigationUI.setupWithNavController(toolbar, navController, appBarConfiguration)
in activity to setup toolbar with navigation component. And that made toolbar responsive for work with navigation internally, pressing "up button" haven't invoked any of overridden methods in activity or fragments.
Instead should be used
NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration)
That will make actionBar responsive for navigation, thus I can use overridden functions onOptionsItemSelected()
and onSupportNavigateUp()
And best place (in my case) to add custom behavior on "up button" click for certain screen is
onSupportNavigateUp()
of hosted activity, like that
override fun onSupportNavigateUp(): Boolean { val navController = this.findNavController(R.id.mainNavHostFragment) return when(navController.currentDestination?.id) { R.id.destinationOfInterest -> { // custom behavior here true } else -> navController.navigateUp() } }
But worth to say, that if you want implement custom behavior directly in fragment, answer of @Enzokie should work like a charm
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With