I'm using Android Navigation component with a navigation drawer (as in Android Studio template). I have fragment A, B, C as top level fragments which are used in navigation drawer and fragment Z which is connected with fragment A in navigation graph. Now I have a button in fragment A. Clicking the button will open the fragment Z using Safe args.
binding.button.setOnClickListener {
val action = NewsFragmentDirections.actionNavNewsToNewsDetailsFragment()
it.findNavController().navigate(action)
}
When the fragment Z is opened, the app bar icon will automatically change to back button which will allow me to go back to fragment A.
These are working fine, but this issue is, when I use the same safe args code in a live data obsedrver, the back button is not working.
viewModel.actionNewsDetails.observe(viewLifecycleOwner, {
val action = NewsFragmentDirections.actionNavNewsToNewsDetailsFragment()
findNavController().navigate(action)
})
Here are some additional details
- Once we are in fragment Z, it will show back navigation as usual, but just clicking it will not do any action
- When I clicked the back button fast for several time, I noticed the app bar title flickering (changing between fragment A and Z)
- I'm able to open Nav drawer by swiping when I'm in fragment Z
- My live data code is written in fragment A's onCreateView()
- The live data is triggered from a function in ViewModel
I've been struggling with this issue for long. Sorry for my bad English.