Hey guys! So, I have been trying out Navigation Co...
# android
t
Hey guys! So, I have been trying out Navigation Components. Created an activity with 2 fragments. A goes to B on button press and B has a button to go back to A by destroying current fragment. Now the issue is on this B to A fragment. If I use
Copy code
binding.button2.setOnClickListener {
            activity?.supportFragmentManager?.popBackStack()
        }
On going back to A, if I use A to B button again, app will crash with this message:
java.lang.IllegalArgumentException: navigation destination dev.theimpulson.myapplication:id/action_firstFragment_to_secondFragment is unknown to this NavController
However, if I use this in B to A button:
Copy code
binding.button2.setOnClickListener {
            activity?.onBackPressed()
        }
App doesn't crashes anymore. Any ideas why?
r
You normally don't interact with the fragment manager yourself when navigating
use
findNavController.navigate(id)
For back/up actions override
onSupportNavigateUp()
in your hosting activity
👍 2
you provide the navcontroller with an
AppbarConfiguration
, this sets your toplevel destinations. Other fragments then get a 'back' arrow in the actionbar to go back. No manual popping of the backstack needed.