I created 4 fragments in my dashboardactivity
1, 2,3,4 And on Back Button Press I must to return from 4 to 1, 3 to 1, 2 to 1
What is the best practise to do that?
My app must not return recently open fragment like 4 to 3, 3 to 4
CodePudding user response:
override on onBackPressed()
method in activity and write this code snippet. I think solve your problem
@Override fun onBackPressed() {
while (supportFragmentManager.getBackStackEntryCount() > 0) {
if (supportFragmentManager.findFragmentById(R.id.container) is Fragment1) {
return
}
getSupportFragmentManager().popBackStackImmediate()
}
}
CodePudding user response:
alternatively you can use Jetpack Navigation, it provides you with a GUI that you can see all the destination in your navigation structure.
https://developer.android.com/guide/navigation? gclid=Cj0KCQiA15yNBhDTARIsAGnwe0UOz85aXEUlPqKU7VnbeGQKAwGufnfxbxTZDNyj6670Oxu3PbkyjtoaAnk4EALw_wcB&gclsrc=aw.ds