I am working with fragment architecture i designed login page in a fragment, i want to remove the only login fragment from back stack after user login successful and open another fragment, please help me.
Asked
Active
Viewed 7,184 times
2
-
wat have you tried? – Shivam Oberoi Jan 04 '18 at 08:02
-
Possible duplicate of https://stackoverflow.com/questions/9033019/removing-a-fragment-from-the-back-stack – Adrián García Campos Jan 04 '18 at 08:03
-
@Adrian, What happens if the fragment that you want to remove is not on top of the stack?, actually i want to remove from back stack from other fragment – Vikas Jan 04 '18 at 08:59
-
@Sihvam, i tried with below code if (((HomeActivity)getActivity()).isFragmentInBackStack(Constants.LOGIN)){ getActivity().getSupportFragmentManager().popBackStackImmediate(Constants.LOGIN,FragmentManager.POP_BACK_STACK_INCLUSIVE); } – Vikas Jan 04 '18 at 09:02
-
Actually i want to remove login fragment from back stack just after successfull login and show dashboard fragment again if we press back then login fragment should not appear. – Vikas Jan 04 '18 at 09:06
3 Answers
2
Try this,
You can add a tag to each fragment while adding them to the backstack and then pop fragment from backstack till the fragment with the tag you want is not reached.
FragmentManager fm = getFragmentManager();
for (int i = fm.getBackStackEntryCount() - 1; i > 0; i--) {
if (!fm.getBackStackEntryAt(i).getName().equalsIgnoreCase(tagname)) {
fm.popBackStack();
}
else
{
break;
}
}

Fenil Patel
- 1,528
- 11
- 28
0
replace()
use replace(), it will replace the login fragment from the back stack before adding the new fragment you want to show
example
Fragment fr1= new GoFragment();
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.id.FragmentContainer,newFragment);
ft.addToBackStack(null);
ft.commit();

Dheeraj Joshi
- 1,522
- 14
- 23
0
When the user Successfully login, do not add the Login Fragment to BackStack, Then you need to Comment below the line.
ft.addToBackStack(null);
This Line adds your Fragment to BackStack when the user navigates from one fragment to another.
Please refer the following link
https://developer.android.com/guide/components/fragments.html
Performing Fragment Transactions Topic

jessica
- 1,700
- 1
- 11
- 17
-
Follow this Link, this may help http://www.andreabaccega.com/blog/2015/08/16/how-to-avoid-fragments-overlapping-due-to-backstack-nightmare-in-android/ – jessica Jan 04 '18 at 09:07