4

I have situation where I want to clear all fragments from backstack except the one which is visible(i.e. on top)

For example, There are four fragments in backstack A->B->C->D (D is on top)

Now I want to remove fragments A,B,C from backstack. But the constraint is there should not be any visible effect on fragment D while removing history from backstack.

This is my code.

FragmentManager fm = getActivity().getSupportFragmentManager();
                Bundle bundle = new Bundle();
                OrderReceiptFragment orderReceiptFragment = new OrderReceiptFragment();
                bundle.putSerializable("orderHistory", orderHistory);
                orderReceiptFragment.setArguments(bundle);
                CommonUtil.clearBackstack(fm);
                fm.beginTransaction().setCustomAnimations(R.anim.enter_from_left,
                        R.anim.exit_to_right)
                        .replace(R.id.container, orderReceiptFragment).commit();

method to clearbackstack

public static void clearBackstack(FragmentManager fragmentManager) {    
    fragmentManager.popBackStack(0, FragmentManager.POP_BACK_STACK_INCLUSIVE);
}

Here the problem is - while clearing the backstack, for some milliseconds the first fragment from the backstack gets visible. Which looks weird. Does anybody have any solution for this?

Abhishek Akhani
  • 209
  • 1
  • 4
  • 9

5 Answers5

5

YOU CAN DO LIKE THIS

Mehtod 1 :: Removing one by one

 FragmentManager fm = getActivity().getSupportFragmentManager();
      for (int i = 0; i < fm.getBackStackEntryCount(); ++i) {
      fm.popBackStack();
}

Mehthod 2

 FragmentManager fm = getActivity().getSupportFragmentManager();
fm .popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE) 

Refer

See this Answer. you can find different methods

Also Refer popBackStackImmediate

Community
  • 1
  • 1
edwin
  • 7,985
  • 10
  • 51
  • 82
  • Did you tried different methods , these are common methods used for clearing backstack. First approcah did you try that – edwin Jun 09 '14 at 13:41
2

How about this-

mFragmentManager.popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE) 
sjain
  • 23,126
  • 28
  • 107
  • 185
1

Try this code:

 FragmentManager _manager = getActivity().getSupportFragmentManager();
_manager.popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE) 
Pankaj Arora
  • 10,224
  • 2
  • 37
  • 59
  • I know about this method and I am using it currently but before starting the fragment D. but user can see the effect i.e. fragment A is visible for a millisecond before fragment D starts. And It looks weird. – Abhishek Akhani Jun 09 '14 at 11:39
-1

If you are trying to make it so that the only layout in the history at any given time is the one that you are currently viewing, you can do the following in the manifest:

android:noHistory="true"

Put that in for each fragment/activity that you don't want to show up in the history.

JRad the Bad
  • 511
  • 5
  • 25
-1

Very Simple:

FragmentManager frgManager;
frgManager = getActivity().getSupportFragmentManager();
frgManager.popBackStack("null", FragmentManager.POP_BACK_STACK_INCLUSIVE);
Ramakishna Balla
  • 1,020
  • 1
  • 8
  • 12
  • Why do you have '0' as the first parameter instead of 'null' ? Any specific reason ? – Ramakishna Balla Jun 09 '14 at 12:03
  • no specific reason... It does the work but the effect is visible as I wrote in my question.. which my client doesn't like... – Abhishek Akhani Jun 09 '14 at 12:15
  • Any 'popping' of fragments -whatever fragment manager you use - makes them "resurrect" for some short period of time (they like get back'ed in a usual way as if user did it by 'back' button). Does not answer question at all – Den Drobiazko Mar 23 '16 at 17:36