lazyguy
lazyguy

Reputation: 963

Custom animation not called when using remove vs hide

I have a fragment which I want to show slide to the left when open and slide to the right when we close it. The show part works fine but when we close it it works if I use hide() but I want to remove the fragment so that it is not shown on configuration change etc but then the same animation working for hide is not working when remove() is called. See the code below.

This code works meaning it shows the panel going to the right animation

Fragment fragment = getFragmentManager().findFragmentByTag(Tags.PANEL_FRAGMENT_TAG.name());
        if (fragment != null) {
            FragmentTransaction transaction = getFragmentManager().beginTransaction();
            transaction.setCustomAnimations(R.animator.slide_in_right, R.animator.slide_out_right);
            transaction.hide(fragment);
            transaction.commit();
        }

This code doesn't work..Any pointers? How can I remove the fragment and animation still works..

 Fragment fragment = getFragmentManager().findFragmentByTag(Tags.PANEL_FRAGMENT_TAG.name());
            if (fragment != null) {
                FragmentTransaction transaction = getFragmentManager().beginTransaction();
                transaction.setCustomAnimations(R.animator.slide_in_right, R.animator.slide_out_right);
                **transaction.remove(fragment);**
                transaction.commit();
            }

Upvotes: 3

Views: 3268

Answers (1)

lazyguy
lazyguy

Reputation: 963

I found the reason in another post at stackoverflow which makes perfect sense

Here is the original post How to animate fragment removal

The reasoning:

"The exiting view is animated on the canvas of the entering view so if there is no entering canvas there is no canvas for the animation.

To show the animation I had to always use replace and use entering fragments of the same size to those exiting. After the animation is finished I set the view of the new fragments to gone."

Upvotes: 2

Related Questions