Here I override handleOnBackPressed() method:
Log.d("Fragment", "onViewCreated")
requireActivity().onBackPressedDispatcher.addCallback(viewLifecycleOwner) {
object : OnBackPressedCallback(true) {
override fun handleOnBackPressed() {
Log.d("Fragment", "Back pressed")
if (binding.crimeTitle.text.isBlank()) {
Toast.makeText(context, "Title can't be empty!", Toast.LENGTH_SHORT).show()
Log.d("Fragment", "Toast showed")
} else {
Log.d("Fragment", "BackStack popped")
findNavController().popBackStack()
}
}
}
}
But after that I see only "onViewCreated" in Logs and nothing happens after pressing "Back" button.
I expected to see at least "Back pressed".
Witout this part of code "Back" button works as usual.
Logs:
(https://i.stack.imgur.com/bAmxq.png)