Solution 1 :
Looks like popBackStack
works asynchronously:
Pop all back stack states up to the one with the given identifier. This function is asynchronous — it enqueues the request to pop, but the action will not be performed until the application returns to its event loop.
(see here)
There’s an alternative popBackStackImmediate.
Problem :
requireActivity().onBackPressedDispatcher.addCallback(viewLifecycleOwner) {
findNavController().popBackStack()
//runs after popping off the back stack
(activity as MainActivity).toolbar.visibility = View.VISIBLE
}
Is it possible to run code after popBackStack() given the fragment is already gone? If I put breakpoints, I found out that (activity as MainActivity).toolbar.visibility = View.VISIBLE runs before the fragment’s onPause() why it is the case?