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.
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.
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.
You need add/commit fragment using one tag ex. "TAG_FRAGMENT". Fragment fragment = getSupportFragmentManager(). findFragmentByTag(TAG_FRAGMENT); if(fragment !=
FragmentManager which is used to create transactions for adding, removing or replacing fragments. fragmentManager. beginTransaction();
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
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
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