I want to animate the removal of fragment.
I tried:
getSupportFragmentManager().beginTransaction() .setCustomAnimations(R.anim.push_down_in, R.anim.push_up_out) .remove(myFragment) .commit();
but the fragment just disappears.
I noticed the out animation only plays with 'replace', so I tried to replace the fragment with an empty Fragment like this:
getSupportFragmentManager() .beginTransaction() .setCustomAnimations(R.anim.push_down_in, R.anim.push_up_out) .replace(viewId, new Fragment()) .commit();
But it still just disappears disappears.
So, how can I animate the removal of fragment?
To animate the transition between fragments, or to animate the process of showing or hiding a fragment you use the Fragment Manager to create a Fragment Transaction . Within each Fragment Transaction you can specify in and out animations that will be used for show and hide respectively (or both when replace is used).
At a high level, here's how to make a fragment transition with shared elements: Assign a unique transition name to each shared element view. Add shared element views and transition names to the FragmentTransaction . Set a shared element transition animation.
To remove a fragment from the host, call remove() , passing in a fragment instance that was retrieved from the fragment manager through findFragmentById() or findFragmentByTag() . If the fragment's view was previously added to a container, the view is removed from the container at this point.
FragmentManager is the class responsible for performing actions on your app's fragments, such as adding, removing, or replacing them, and adding them to the back stack.
I saw this when I was having similar problems and just thought Id drop a quick note.
Rather than creating a dummy fragment in order to replace the existing one I think you should animate the current fragments view. When the animation finishes you can simply remove the fragment.
This is how i did it:
final FragmentActivity a = getSherlockActivity(); if (a != null) { //Your animation Animation animation = AnimationUtils.loadAnimation(a, R.anim.bottom_out); animation.setDuration(getResources().getInteger(android.R.integer.config_shortAnimTime)); //You can use AnimationListener, MagicAnimationListener is simply a class extending it. animation.setAnimationListener(new MagicAnimationListener() { @Override public void onAnimationEnd(Animation animation) { //This is the key, when the animation is finished, remove the fragment. try { FragmentTransaction ft = a.getSupportFragmentManager().beginTransaction(); ft.remove(RestTimerFragment.this); ft.commitAllowingStateLoss(); } catch (Exception e) { e.printStackTrace(); } } }); //Start the animation. getView().startAnimation(animation); }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With