Android - How to animate an activity transition when the default back button is pressed

試著忘記壹切 提交于 2019-11-28 06:43:58
TaoZang

maybe you can do this work in onBackPressed() method in the activity.

@Override
public void onBackPressed() {
    super.onBackPressed();
    overridePendingTransition(R.anim.comming_in, R.anim.comming_out);   
}

Basically overriding onBackPressed is a proper approach, but rather than call finish() from it i would say that is better to call super.onBackPressed() and then add overridePendingTransition so we are a bit more consistent with the inheritance 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 would suggest overriding the finish() method, just in case the activity is finished in some other way, like a navigation action or any other view action that "destroys" the activity:

@Override public void finish() {
   super.finish();
   overridePendingTransition(0,0);
}

We need to have in consideration that this method will be triggered after the back button has been pressed, so we are good to go :-)

Update: Moreover, overriding onBackPressed() could mess up with the Activity if we are using fragments in it, because we probably don't want to be overriding the transitions every time the back is pressed.

if you use fragment you can proceed like 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>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!