Reputation: 329
Suppose I have a HomeActivity
which is making all the FragmentTransaction
. So, In each fragment when required to open another fragment.
Firstly fragment goes back to the HomeActivity using newIntent() with bundle. Then Home Activity opens the required fragment requested by previous fragment according to the data in bundle. After opening so many fragments by the same way when hits back button it takes me back to all the previous fragments including the current from which I pressed back(if I opened it on more than one time).
So, how to finish() the current fragment in which I hits back from all the previous fragment history, so that I can't see it again by going to previous fragments
Upvotes: 0
Views: 2365
Reputation: 329
Thanx to all for giving me your knowledgeable answers but i find the an another solution which is I defined a public static boolean variable in the fragment which I wanted should not be appeared twice or more in back stack. So Whenever that fragment is destroyed, the value of boolean variable becomes true(And then in onStart() it checks the value whenever it comes from back stack and take further action) which prevents that fragment to be appeared again when hitting the back.Again thanx to all for your participation.
Upvotes: 0
Reputation: 1090
You are adding Fragment in Your Stack, if you are add Fragment in backstack by fragmentTransaction.addToBackStack("[Fragment Name as Flag]");
so your transaction will be remember and you will navigate back on fragment which is Top on stack.
So when you don't want add fragment in backStack so, don't use fragmentTransaction.addToBackStack("[Fragment Name as Flag]");
during fragment transactions.
Upvotes: 1
Reputation: 2437
just remove this code from your fragment transaction
addToBackStack("null")
i hope it will help you.
Upvotes: 1
Reputation: 510
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(containerId, fragment, fragment.getClass().getSimpleName());
fragmentTransaction.addToBackStack("[Fragment Name as Flag]");
fragmentTransaction.commit();
just add backstack with your fragment Tag, so the Fragment transaction will count opened same fragment as 1, and when popBackStack, it will clear all the stack with same Flag
Upvotes: 1