I'm writing android app, using fragments.
There are two main ways to navigate up or back.
The first is to use the arrow on the navigation bar (marked with the number 1 in the picture)
The second use the system back button (marked with the number 2)
I wrote:
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View {
...
requireActivity().onBackPressedDispatcher.addCallback(this) {
findNavController().navigate(R.id.action_updateFragment_to_listFragment)
...
}
and it works just fine, but only for system back button(2).
How can I override action caused by click on the back arrow from navigation bar(1)?
thanks!!!
CodePudding user response:
Ok, I've got this.
override fun onOptionsItemSelected(item: MenuItem): Boolean {
if(item.itemId == android.R.id.home) {
Toast.makeText(requireContext(), "the answer", Toast.LENGTH_SHORT).show()
}
return super.onOptionsItemSelected(item)
}
I hope this post will help someone in the future.