[ACCEPTED]-Android - How to animate an activity transition when the default back button is pressed-back
maybe you can do this work in onBackPressed() method 1 in the activity.
@Override
public void onBackPressed() {
super.onBackPressed();
overridePendingTransition(R.anim.comming_in, R.anim.comming_out);
}
Basically overriding onBackPressed is a 5 proper approach, but rather than call finish() from 4 it i would say that is better to call super.onBackPressed() and 3 then add overridePendingTransition so we 2 are a bit more consistent with the inheritance 1 rules.
@Override
public void onBackPressed() {
super.onBackPressed();
overridePendingTransition(R.anim.comming_in, R.anim.comming_out);
}
Even though overriding onBackPressed()
is a good option, I 12 would suggest overriding the finish()
method, just 11 in case the activity is finished in some 10 other way, like a navigation action or any 9 other view action that "destroys" the activity:
@Override public void finish() {
super.finish();
overridePendingTransition(0,0);
}
We 8 need to have in consideration that this 7 method will be triggered after the back 6 button has been pressed, so we are good 5 to go :-)
Update: Moreover, overriding onBackPressed()
could mess 4 up with the Activity if we are using fragments 3 in it, because we probably don't want to 2 be overriding the transitions every time 1 the back is pressed.
if you use fragment you can proceed like 1 this :
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.setCustomAnimations(R.anim.anim_slide_in_left, R.anim.anim_slide_out_left, R.anim.anim_slide_out_right, R.anim.anim_slide_in_right);
transaction.replace(R.id.fragment_container, new YourClassFragment);
transaction.addToBackStack(null);
transaction.commit();
anim_slide_in_left
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<translate
android:duration="500"
android:interpolator="@android:interpolator/decelerate_quint"
android:fromXDelta="100%p"
android:toXDelta="0%p" >
</translate>
</set>
anim_slide_out_left
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<translate
android:duration="500"
android:interpolator="@android:interpolator/decelerate_quint"
android:fromXDelta="0%p"
android:toXDelta="-100%p" >
</translate>
</set>
anim_slide_out_right
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<translate
android:duration="@android:integer/config_mediumAnimTime"
android:interpolator="@android:interpolator/decelerate_quint"
android:fromXDelta="-100%p"
android:toXDelta="0%p" >
</translate>
</set>
anim_slide_in_right
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<translate
android:duration="@android:integer/config_mediumAnimTime"
android:interpolator="@android:interpolator/decelerate_quint"
android:fromXDelta="0%p"
android:toXDelta="100%p" >
</translate>
</set>
More Related questions
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.