Android - 如何在按下默认后退按钮时为活动过渡设置动画

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12047770/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 09:35:04  来源:igfitidea点击:

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

androidanimationbuttontransitionback

提问by Tiago

In my activity, I have a button with the following click listener that is working great:

在我的活动中,我有一个带有以下单击侦听器的按钮,效果很好:

final ImageButton startOverButton = (ImageButton) findViewById(R.id.start_over_button);
startOverButton.setOnClickListener(new View.OnClickListener(){

    @Override
    public void onClick(final View v) {

        finish();//go back to the previous Activity
        overridePendingTransition(R.anim.comming_in, R.anim.comming_out);
    }
});

It animates the return to the previous activity the way I want. However, when the user presses the Android default back button, the animation is not triggered. My question is: where should I put the animation code overridePendingTransition(R.anim.comming_in, R.anim.comming_out);so that this animation will be triggered both when the user clicks on my button and in the default Android back button?

它以我想要的方式动画返回上一个活动。但是,当用户按下 Android 默认的后退按钮时,不会触发动画。我的问题是:我应该在哪里放置动画代码overridePendingTransition(R.anim.comming_in, R.anim.comming_out); 以便在用户单击我的按钮和默认的 Android 后退按钮时触发此动画?

As a naive try, I have tried to put the overridePendingTransition(R.anim.comming_in, R.anim.comming_out);line of code in the onDestroy()method but it did not work.

作为一个天真的尝试,我试图将overridePendingTransition(R.anim.comming_in, R.anim.comming_out); onDestroy()方法中的代码行,但它不起作用。

Thank you in advance!

先感谢您!

回答by TaoZang

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

也许您可以在活动中的 onBackPressed() 方法中完成这项工作。

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

回答by Caye

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.

基本上覆盖 onBackPressed 是一种正确的方法,但与其调用 finish() 相比,我会说最好调用 super.onBackPressed() 然后添加 overridePendingTransition 以便我们更符合继承规则。

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

回答by cesards

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:

即使覆盖onBackPressed()是一个不错的选择,我还是建议覆盖该finish()方法,以防万一活动以其他方式完成,例如导航操作或“破坏”活动的任何其他视图操作:

@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.

更新:此外,onBackPressed()如果我们在其中使用片段,覆盖可能会弄乱 Activity,因为我们可能不想在每次按下后退时覆盖转换。

回答by amine

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

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

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

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

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>