How to pop fragment off backstack

You can pop the fragment by name. While adding fragments to the back stack, just give them a name. fragmentTransaction.addToBackStack(“fragB”); fragmentTransaction.addToBackStack(“fragC”); Then in Fragment_C, pop the back stack using the name ie.. fragB and include POP_BACK_STACK_INCLUSIVE someButtonInC.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { FragmentManager fm = getActivity() .getSupportFragmentManager(); fm.popBackStack (“fragB”, FragmentManager.POP_BACK_STACK_INCLUSIVE); } });

Android: Fragments backStack

If you really want to replace the fragment then use replace() methode instead of doing a remove() and an add(). FragmentManager fragmentManager = getFragmentManager(); FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); fragmentTransaction.replace(…………..); fragmentTransaction.addToBackStack(null); fragmentTransaction.commit(); Don’t forget to do the addToBackStack(null) so your previous state will be added to the backstack allowing you to go back with the back … Read more

Android: open activity without save into the stack

When starting your list’s Activity, set its Intent flags like so: Intent i = new Intent(…); // Your list’s Intent i.setFlags(i.getFlags() | Intent.FLAG_ACTIVITY_NO_HISTORY); // Adds the FLAG_ACTIVITY_NO_HISTORY flag startActivity(i); The FLAG_ACTIVITY_NO_HISTORY flag keeps the new Activity from being added to the history stack. NB: As @Sam points out, you can use i.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY); instead. There is … Read more

get the latest fragment in backstack

You can use the getName() method of FragmentManager.BackStackEntry which was introduced in API level 14. This method will return a tag which was the one you used when you added the Fragment to the backstack with addTobackStack(tag). int index = getActivity().getFragmentManager().getBackStackEntryCount() – 1 FragmentManager.BackStackEntry backEntry = getFragmentManager().getBackStackEntryAt(index); String tag = backEntry.getName(); Fragment fragment = getFragmentManager().findFragmentByTag(tag); … Read more

Fragments onResume from back stack

For a lack of a better solution, I got this working for me: Assume I have 1 activity (MyActivity) and few fragments that replaces each other (only one is visible at a time). In MyActivity, add this listener: getSupportFragmentManager().addOnBackStackChangedListener(getListener()); (As you can see I’m using the compatibility package). getListener implementation: private OnBackStackChangedListener getListener() { OnBackStackChangedListener … Read more

Android: Remove all the previous activities from the back stack

The solution proposed here worked for me: Java Intent i = new Intent(OldActivity.this, NewActivity.class); // set the new task and clear flags i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK); startActivity(i); Kotlin val i = Intent(this, NewActivity::class.java) // set the new task and clear flags i.flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK startActivity(i) However, it requires API level >= 11.

Android – save/restore fragment state

When a fragment is moved to the backstack, it isn’t destroyed. All the instance variables remain there. So this is the place to save your data. In onActivityCreated you check the following conditions: Is the bundle != null? If yes, that’s where the data is saved (probably orientation change). Is there data saved in instance … Read more

tech