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: 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

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

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

Problems with Android Fragment back stack

Explanation: on what’s going on here? If we keep in mind that .replace() is equal with .remove().add() that we know by the documentation: Replace an existing fragment that was added to a container. This is essentially the same as calling remove(Fragment) for all currently added fragments that were added with the same containerViewId and then … Read more

How to prevent multiple instances of an Activity when it is launched with different Intents

Add this to onCreate and you should be good to go: // Possible work around for market launches. See https://issuetracker.google.com/issues/36907463 // for more details. Essentially, the market launches the main activity on top of other activities. // we never want this to happen. Instead, we check if we are the root and if not, we … 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.