0

I'm trying to make a correct BackStack. I have tried to implement this decision. But it does not work for me.

I have one list in navigation drawer and other navigation. So, when I select different fragment lists a few times, I want one-touch return to the main menu instead of returning to BackStack.enter image description here

@Override
public void onBackPressed() {
    if (getSupportFragmentManager().getBackStackEntryCount() == 1){
        finish();
    }
    else {
        super.onBackPressed();
    }
}


@Override
public void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    outState.putInt("theme", mThemeId);
}
private void replaceFragment (Fragment fragment){
    String backStateName =  fragment.getClass().getName();
    String fragmentTag = backStateName;

    FragmentManager manager = getSupportFragmentManager();
    boolean fragmentPopped = manager.popBackStackImmediate(backStateName, 0);

    if (!fragmentPopped && manager.findFragmentByTag(fragmentTag) == null) {
        FragmentTransaction ft = manager.beginTransaction();
        ft.setCustomAnimations(R.anim.slide_in_left, R.anim.slide_out_right,
                R.anim.slide_back_in_left, R.anim.slide_back_out_right);
        ft.replace(R.id.frgmCont, fragment);
        ft.addToBackStack(backStateName);
        ft.commit();
    }


}
Community
  • 1
  • 1
GensaGames
  • 5,538
  • 4
  • 24
  • 53

1 Answers1

0

You can start by defining these two methods:

A method to push a fragment transaction with a flag that defines if the fragment should be added to the backStack or not:

private void pushToStack(Fragment fragment, String tag, int transition, boolean addToBackStack){
    FragmentManager fm = this.getSupportFragmentManager();

    FragmentTransaction transaction = fm.beginTransaction();
    transaction.replace(R.id.container, fragment, tag);

    if(transition > 0){
        transaction.setTransition(transition);
    }
    if(addToBackStack){
        transaction.addToBackStack(tag);
    }

    transaction.commit();
}

A method that clears out the backStack:

public void clearStack(){
    FragmentManager fm = this.getSupportFragmentManager();
    fm.popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
}

Then you only need to handle the following 3 cases:

1) When the activity is newly created you won't add the fragment to the backstack:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.setContentView(R.layout.activity_home);
    this.pushToStack(DefaultHomeFragment.newInstance(), DefaultHomeFragment.class.getSimpleName(), -1, false);
}

2) When you want to add a new fragment to the navigation flow you call the pushToStack method with the addToBackStack set to true:

this.pushToStack(NavigationSecondFragment.newInstance(), NavigationSecondFragment.class.getSimpleName(), DEFAULT_TRANSITION, true);
this.pushToStack(NavigationSecondFragment.newInstance(), NavigationThirdFragment.class.getSimpleName(), DEFAULT_TRANSITION, true);

3) If the wanted fragment to be pushed is the DefaultHomeFragment, clear the stack and then add it as the first time with something like this:

private static final String DEFAULT_CONTENT_NAME = DefaultHomeFragment.class.getSimpleName();

public addFragmentToNavigation(Fragment newFragment){
    boolean addToBackStack = true;

    if(newFragment..getClass().getSimpleName().equals(DEFAULT_CONTENT_NAME)){
        addToBackStack = false;
        this.clearStack();
    }

    this.pushToStack(newFragment, newFragment.getClass().getSimpleName(),
        DEFAULT_TRANSITION, addToBackStack);
}

This code might give you a better idea: https://github.com/GrzegorzFeather/open-mbsurfer

Jorge Mendez
  • 674
  • 4
  • 15