Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change animation of fragment already in stack

Assume I run the following code to place a fragment into the stack. It sets the animations for when I initially view the fragment and for when the fragment is exited.

FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.setCustomAnimations(R.anim.slide_left,
                                        R.anim.no_anim,
                                        R.anim.no_anim_show,
                                        R.anim.slide_right_away);
fragmentTransaction.replace(R.id.container, fragment, tag);
fragmentTransaction.addToBackStack(tag);
fragmentTransaction.commit();

If i want to change the exit animation at a later time, how could I do this? As in, if I had the following code:

getFragmentManager().popBackStack();

But I want a different animation then the one I originally placed into the stack.

like image 393
graviton Avatar asked Mar 17 '14 20:03

graviton


People also ask

How to transition between fragments?

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 to replace fragment with activity in android?

Use replace() to replace an existing fragment in a container with an instance of a new fragment class that you provide. Calling replace() is equivalent to calling remove() with a fragment in a container and adding a new fragment to that same container.

How to remove fragment from FragmentManager?

You need add/commit fragment using one tag ex. "TAG_FRAGMENT". Fragment fragment = getSupportFragmentManager(). findFragmentByTag(TAG_FRAGMENT); if(fragment !=

Which kind of fragment transaction is used for removing a fragment from the screen?

FragmentManager which is used to create transactions for adding, removing or replacing fragments. fragmentManager. beginTransaction();


2 Answers

The following is what I tried, and it worked:

getFragmentManager().beginTransaction().setCustomAnimations(
        R.anim.slide_left, R.anim.new_anim)
        .remove(fragment)
        .commit();

So, it looks like I had to explicitly 'remove' the fragment in a new fragment transaction, in order to make use of the new exit animation

like image 54
Rajath Avatar answered Oct 01 '22 01:10

Rajath


You can change the custom animation before popping from back stack

getFragmentManager().beginTransaction().setCustomAnimations(R.anim.slide_left,
            R.anim.no_anim,
            R.anim.no_anim_show,
            R.anim.new_pop_exit).commit();

and execute pending transactions before popping back stack

getFragmentManager().executePendingTransactions();
getFragmentManager().popBackStackImmediate();

Note : popExit animations will be played for exit operations specifically when popping the back stack

like image 34
Libin Avatar answered Oct 01 '22 02:10

Libin