I'm using the Navigation Components and am trying to just listen to the back button from a fragment; Before listening, everything works fine meaning both the toolbar button and the native back button navigate to the previous fragment as expected.
So, to add a listener, I followed this: https://developer.android.com/guide/navigation/navigation-custom-back and added a callback in my fragment like so:
override fun onCreate(savedInstanceBundle: Bundle?){
// saveIfNew when back button is pressed
requireActivity().onBackPressedDispatcher.addCallback(this){
viewModel.checkAndSave()
}
}
After doing that, the native back button no longer works, but the toolbar back does work as expected. The docs on that page seem to make it seem like this was just a listener and shouldn't be affecting the navigation chain, so what am I doing wrong here??