0

I have two questions:

  1. I want to remove all fragments in back-stack when user come back to main page. However when I call following statement there is a flickering on the main page.

        fragmentManager.popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
    

    how to remove this flickering? I tried the method here no luck. Pop the fragment backstack without playing the Pop-Animation*

  2. In fragment transition i use replace method. But in some transitions I dont want to reload the entire data when user presses the back button. To implement this I used hide() and add() methods. When this is done above back-stack remove process becomes really bad with so many animations.

Is there a best practice to implement this?

Community
  • 1
  • 1
pats
  • 1,163
  • 2
  • 19
  • 37

2 Answers2

1

I had exactly the same issue as (1). Here is what worked for me:

  • the transaction that shows the first fragment is added to the backstack using a named backstate, eg: "bottom".
  • the backstack is cleared with popBackStackImmediate("bottom", ...INCLUSIVE).

So, whenever I want to replace whatever is in the backstack with a new fragment I use the following function:

protected void showInitialFragment(Fragment fragment) {
    getSupportFragmentManager()
            .popBackStackImmediate(BOTTOM_BACK_STATE, FragmentManager.POP_BACK_STACK_INCLUSIVE);

    getSupportFragmentManager()
            .beginTransaction()
            .add(R.id.fragment_content, fragment)
            .addToBackStack(BOTTOM_BACK_STATE)
            .commit();
}

I also had to override onBackPressed() like this:

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

Hope this helps!

0

Found an easy method https://stackoverflow.com/a/67005552/7418129. Sorry if I'm late. But it will definitely help someone.

ABHIMANGAL MS
  • 974
  • 12
  • 17