Pop the Fragment Backstack Without Playing the Pop-Animation

Pop the fragment backstack without playing the Pop-Animation

So Warpzit was on the right track, he just didn't address your specific issue too well. I came across the exact same issue and here is how I solved it.

First I created a static boolean variable (for simplicity's sake, lets put it in the FragmentUtils class)...

public class FragmentUtils {
public static boolean sDisableFragmentAnimations = false;
}

Then, in EVERY fragment you have, you need to override the onCreateAnimation method...

@Override
public Animation onCreateAnimation(int transit, boolean enter, int nextAnim) {
if (FragmentUtils.sDisableFragmentAnimations) {
Animation a = new Animation() {};
a.setDuration(0);
return a;
}
return super.onCreateAnimation(transit, enter, nextAnim);
}

Then, when you need to clear the backstack from your activity simply do the following...

public void clearBackStack() {
FragmentUtils.sDisableFragmentAnimations = true;
getSupportFragmentManager().popBackStackImmediate(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
FragmentUtils.sDisableFragmentAnimations = false;
}

And voila, a call to clearBackStack() will drop you back into the root fragment without any transition animations.

Hopefully the big G will add a less stupid way of doing this in the future.

Fragment popbackstack animation not working

If you look at the code you are replacing the fragment with a new fragment but you're actually setting add to back stack null. It's good practice to provide a tag for each fragment, and it's even going to be easy to find that fragment by tag as well. Add tags to your fragment like below. And if it still doesn't work then the problem will be inside your animation xml files.

private void fragmentAppear(){
fragment = new LoginFragment();
fragmentManager = LoginActivity.this.getSupportFragmentManager();
fragmentTransaction = fragmentManager.beginTransaction();
//my XML anim files
fragmentTransaction.setCustomAnimations(
R.anim.slide_in_bottom,0,0,R.anim.slide_out_bottom);
fragmentTransaction.replace(
R.id.login_fragment, fragment, "loginFragment");
fragmentTransaction.addToBackStack("loginFragment");
fragmentTransaction.commit();
}

From the Fragment Transaction documentation, I see this function, and there you have to specify appropriate animations.

/**
* Set specific animation resources to run for the fragments that are
* entering and exiting in this transaction. The
* <code>popEnter</code>
* and <code>popExit</code> animations will be played for enter/exit
* operations specifically when popping the back stack.
*/
public abstract FragmentTransaction setCustomAnimations(@AnimRes int enter,
@AnimRes int exit, @AnimRes int popEnter, @AnimRes int popExit);
  1. enter => Animation when fragment enter
  2. exit => Animation when fragment exits.
  3. popEnter => Animation when fragment enters from back stack.
  4. popExit => Animation when fragment exit when popping from back stack.

Play with these until you get your desired behavior.

How to pop fragment off backstack

You can pop the fragment by name. While adding fragments to the back stack, just give them a name.

fragmentTransaction.addToBackStack("fragB");
fragmentTransaction.addToBackStack("fragC");

Then in Fragment_C, pop the back stack using the name ie.. fragB and include POP_BACK_STACK_INCLUSIVE

someButtonInC.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {

FragmentManager fm = getActivity()
.getSupportFragmentManager();
fm.popBackStack ("fragB", FragmentManager.POP_BACK_STACK_INCLUSIVE);
}
});

Manage Fragment Backstack Flow without flicks

I was so curious about this question that i created a sample project and implemented the same use-case that you mentioned in your question. Here is how i handled this.

Used this method to remove F,E,D fragments from backstack

private void removeFragments() {
getSupportFragmentManager().popBackStack("F", FragmentManager.POP_BACK_STACK_INCLUSIVE);
getSupportFragmentManager().popBackStack("E", FragmentManager.POP_BACK_STACK_INCLUSIVE);
getSupportFragmentManager().popBackStack("D", FragmentManager.POP_BACK_STACK_INCLUSIVE);
}

Used this method to replace fragment

private void replaceNewFragment(String key) {
getSupportFragmentManager().beginTransaction().addToBackStack(key)
.replace(android.R.id.content, AFragment.newInstance(key)).commit();
}

Here is a video demo video.
Sample Image

Here is complete of this project on github

Clear Android Fragment back stack without popping?

You could try the follow, though I am unfamiliar with .remove() it seems that it should do what you want:

myFragmentClass myFragC1 = (myFragmentClass) getFragmentManager().findFragmentByTag("theTagYouUsedWhenAddingToBackStack");
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.remove(myFragC1);
transaction.commit();

Android pop fragment from backstack with animation

If you want backstack animations you'll need to use:

setCustomAnimations(int enter, int exit, int popEnter, int popExit)

Something like:

setCustomAnimations(R.anim.enter_from_left, R.anim.enter_from_left,
R.anim.exit_to_left, R.anim.exit_to_left);

How can I pop fragment from backstack without resuming it?

It's architectural question.

1)Suppose we have 2 controllers (fragments/activities): controller A and controller B.
Both of them connected to one instance of some Model (you may use binding with service, Singleton pattern or init model in Application-heir class and make a getter to it).
When something interesting happens in controller B, he notifies model about it and then model calls controllerA.finish() / controllerA.remove().
Of course, I always try to implement this solution as nicely as possible, but there is a main idea.

2)In another circumstances, I call finish() immediately after startActivity(intent);

3)Also I may write: startActivityForResult(intent, requestCode) and after finishing second activity method onActivityResult(requestCode, responseCode, intent) is called - if requestCode's are equal, I finish the activity.



Related Topics



Leave a reply



Submit