[英]Android app overridePendingTransition not shown
I have an app with the Main Activity
using WebView
to load a webpage. 我有一个使用WebView
加载网页的Main Activity
的应用程序。
I am trying to set animation when the user wants to go back to the previous webpage using the back button on the device (not a back button on the screen) but the animation is not shown at all. 当用户希望使用设备上的后退按钮(而不是屏幕上的后退按钮)返回上一个网页时,我试图设置动画,但是动画根本没有显示。
The code below is outside of onCreate()
. 下面的代码在onCreate()
。
NOTE: Notice that i tried overridePendingTransition
in each line i commented out with no any effect. 注意:请注意,我在注释掉的每一行中都尝试了overridePendingTransition
,没有任何效果。
Here is the Code: 这是代码:
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
Log.i("BACK", " Back pressed!");
//overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_left);
if ((keyCode == KeyEvent.KEYCODE_BACK) && this.webView.canGoBack()) {
//overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_left);
this.webView.goBack();
return true;
}
else{
return super.onKeyDown(keyCode, event);
}
}
@Override
public void onBackPressed(){
super.onBackPressed();
//overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_left);
//finish();
return;
}
If I use finish()
it exits the app instead of going back. 如果我使用finish()
则退出应用程序而不是返回。
Here the slide_in_right xml: 这里的slide_in_right xml:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" android:interpolator="@android:anim/accelerate_interpolator">
<translate
android:fromXDelta="100%"
android:toXDelta="0%"
android:duration="500" />
<alpha android:duration="500" android:fromAlpha="0.0" android:toAlpha="1.0" />
</set>
And finally the slide_out_left: 最后是slide_out_left:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:fromXDelta="0%"
android:toXDelta="-100%"
android:duration="500" />
<alpha android:duration="500" android:fromAlpha="1.0" android:toAlpha="0.0" />
</set>
I am not sure what i am missing. 我不确定我缺少什么。 I have already checked a lot of the topics but i couldn't fix mine issue. 我已经检查了很多主题,但无法解决我的问题。
Thanks in advance. 提前致谢。
Remove super.onBackPressed();
删除super.onBackPressed();
from public void onBackPressed()
because it will give a super call and activity gets finished there only. 来自public void onBackPressed()
因为它将发出超级调用,并且仅在此处完成活动。
Change your onBackPressed() code to : 将您的onBackPressed()代码更改为:
@Override
public void onBackPressed(){
finish();
overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_left);
}
Yes, as mentioned by Kishore Jethava. 是的,正如Kishore Jethava所说。 You should call finish() first and then onerridingPendingTransition(..) 您应该先调用finish(),然后再调用onerridingPendingTransition(..)
here is the solution!!! 这是解决方案!!!
call overridePendingTransition after finishing Activity
完成Activity
后,调用overridePendingTransition
@Override
public void onBackPressed() {
super.onBackPressed();
finish();
overridePendingTransition(R.anim.activity_backin, R.anim.activity_back_out);
}
activity_backin.xml in anim
folder anim
文件夹中的activity_backin.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:duration="200"
android:fromXDelta="-100%"
android:toXDelta="0%" />
</set>
activity_back_out.xml in anim
folder anim
文件夹中的activity_back_out.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:duration="200"
android:fromXDelta="0%"
android:toXDelta="100%" />
</set>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.