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,273
  • 2
  • 20
  • 43

3 Answers3

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.

user7418129
  • 1,074
  • 14
  • 18
0

The best solution for me was to add setReorderingAllowed(true).

Example:

parentFragmentManager.commit {
      setReorderingAllowed(true)
      addToBackStack(null)
      setCustomAnimations(R.anim.enter_right, R.anim.exit_left, R.anim.enter_left, R.anim.exit_right)
      replace<AnotherFragment>(R.id.fragment_container)
}
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jul 07 '22 at 03:19