Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to animate fragment removal

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?

like image 816
hugoc Avatar asked Dec 20 '12 23:12

hugoc


People also ask

How do you animate a 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).

How do you animate fragment transitions?

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.

How do I remove a fragment from a transaction?

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.

What is the use of FragmentManager in Android?

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.


1 Answers

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); } 
like image 60
zoltish Avatar answered Oct 02 '22 16:10

zoltish