i have 5 fragments and I use the following code to setCustomAnimations for FragmentTransaction:
FragmentTransaction trans = getFragmentManager().beginTransaction();
trans.setCustomAnimations(android.R.anim.slide_in_left, android.R.anim.slide_out_right);
but how to setCustomAnimations for this one:
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK ) {
if (getSupportFragmentManager().getBackStackEntryCount() == 0)
{
this.finish();
return false;
}
else
{
getSupportFragmentManager().popBackStack();
return false;
}
}
return super.onKeyDown(keyCode, event);
}
At runtime, a FragmentManager can add, remove, replace, and perform other actions with fragments in response to user interaction. Each set of fragment changes that you commit is called a transaction, and you can specify what to do inside the transaction using the APIs provided by the FragmentTransaction class.
By calling addToBackStack(), the replace transaction is saved to the back stack so the user can reverse the transaction and bring back the previous fragment by pressing the Back button.
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.
There is a second setCustomAnimations method (here's the support library method) that has two extra IDs present for the inclusion of the animation to be used when you pop the back stack. Pass the animation IDs you wish to occur when the back stack is popped (transaction is reversed) in the last to spots.
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.setCustomAnimations(android.R.anim.slide_in_left,
android.R.anim.slide_out_right,
ANIMATION_ID_FOR_ENTERING_VIEW,
ANIMATION_ID_FOR_EXITING_VIEW);
Then when you call
getFragmentManager().popBackStack();
The animations will play, the third animation passed to that method will play for the view that you originally removed, and the fourth will play for the view that was visible and is being removed. From the current animations you have for the initial transaction, I would guess you want to use android.R.anim.slide_in_right and android.R.anim.slide_out_left for the back stack animations (see below):
transaction.setCustomAnimations(android.R.anim.slide_in_left,
android.R.anim.slide_out_right,
android.R.anim.slide_in_right,
android.R.anim.slide_out_left);
You only have to call setCustomAnimations when initially adding the fragment to the stack (just like you are doing now, simply add the extra animation IDs), the back stack will remember the animations you set and will automatically play them when you pop back.
NOTE: This method is only available in API 13 and above unless you are using the support v4 jar, which it looks like you are (as you are using getSupportFragmentManager() instead of the regular method).
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