-1

I am trying to manage fragments in a very simple way. I have created a utility class that's adding fragments to the backstack and when we press the back button, the previous fragment shows up. I am OK with that. But, when I am trying to clear out all the fragments, and show up the root fragment, I am not able to do so properly. Following is my utility class:

public class FragmentUtil {
private FragmentUtil() {
}

public static void animatedReplace(FragmentActivity activity, int containerId, Fragment fragment, Bundle args, boolean addToBackStack){
    FragmentTransaction transaction=activity.getSupportFragmentManager().beginTransaction();
    fragment.setArguments(args);
    if(addToBackStack){
        transaction.addToBackStack(fragment.getClass().getName());
    }
    transaction.setCustomAnimations(R.anim.slide_from_right, R.anim.slide_to_left,R.anim.slide_from_left,R.anim.slide_to_right);
    transaction.replace(containerId,fragment);
    transaction.commit();
}

public static void clearBackStackToHome(FragmentActivity activity){
    FragmentManager manager = activity.getSupportFragmentManager();
    manager.popBackStack(DashboardFragment.class.getName(),FragmentManager.POP_BACK_STACK_INCLUSIVE);
}
}

When I want to another fragment I just pass "true" for addToStack in animatedReplace method. When I want to clear the backstack I pass "false" and call the clearBackStackToHome method. Can anybody help me what I am doing wrong?

Hitesh Pamnani
  • 177
  • 3
  • 14

3 Answers3

0

make a condition where check backStack is empty or not. when all fragments are removed from backStack then show your root fragment

Aashutosh Kumar
  • 183
  • 4
  • 12
0

Try this:

in your Utility class:

public void clearBackStack() {
    FragmentManager manager = activity.getSupportFragmentManager();
    if (manager.getBackStackEntryCount() > 0) {
        FragmentManager.BackStackEntry first = manager
                .getBackStackEntryAt(0);
        manager.popBackStack(first.getId(),
                FragmentManager.POP_BACK_STACK_INCLUSIVE);
    }
}

make sure in your root/first fragment animatedReplace() you pass boolean addToBackStack = false .

so that it will not be cleared.

EDIT

To use it in OnBackPressed() overwrite onBackPressed() in your activity and add the following code:

 @Override
    public void onBackPressed() {
        FragmentManager fm = getSupportFragmentManager();
        if (fm.getBackStackEntryCount() > 0) {
             FragmentManager.BackStackEntry first = fm
                    .getBackStackEntryAt(0);
            fm.popBackStack(first.getId(),
                    FragmentManager.POP_BACK_STACK_INCLUSIVE);
            Log.i("MainActivity", "popping backstack");
        } else {
             super.onBackPressed(); 
        }
    }
rafsanahmad007
  • 23,683
  • 6
  • 47
  • 62
  • Hi rafsanahmad007! The solution is working pretty well. But, how to implement it on onBackPressed()? – Hitesh Pamnani Apr 01 '17 at 07:31
  • It will happen everytime the user taps back button. I want to happen only after a certain process. For example, after completing the payment process, the user can tap back button and navigates to root fragment. – Hitesh Pamnani Apr 01 '17 at 07:52
  • you can add a if condition...at the `onBackPressed()` so that if the condition satisfies..then the inner code will execute – rafsanahmad007 Apr 01 '17 at 07:55
0

Yes, you can manage fragment back press in onBackPressed() method of your MainActivity. just override onBackPressed() , and check backStack count, if the count is zero then navigate to your root fragment or exit from app. Also, you can implement your code according to check which fragment in back stack.

@Override
public void onBackPressed() {


    FragmentManager fm = getSupportFragmentManager();
    int count = fm.getBackStackEntryCount();
    if (count >= 1) {
        if (fm.findFragmentById(R.id.main_container) instanceof yourFragment) {

            super.onBackPressed();

        }
    } else {
        // Show your root fragment  here
    }

}
Nishchal Sharma
  • 1,070
  • 9
  • 16