The default animation when the Back button is pressed is a slide from left to right. I'd like to replace that with a custom animation. I'm currently thinking that some combination of onBackPressed() and overridePendingTransition will do the trick, but I haven't been able to get it working.
-
Include some sample code, otherwise we have no idea what you may be doing wrong.prestomation– prestomation2010-07-20 19:59:21 +00:00Commented Jul 20, 2010 at 19:59
Add a comment
|
4 Answers
I think you shouldn't use finish() because the data stored by the Views will be erased
@Override
public void onBackPressed() {
super.onBackPressed();
overridePendingTransition(R.anim.zoom_enter, R.anim.zoom_exit);
}
4 Comments
Chris.Jenkins
Take note of the super, it needs to be AFTER it, otherwise no effect.
NT_
@Chris.Jenkins If you are telling that
overridepending.. should be after the super , Then yes. It should be after any finishing calls . the super basically calls a finish . so the overrride should be after finish or the super in this case .phreakhead
Now, is there a way to do this EXCEPT on the last back button press which would quit your app? i.e. is there a way to check the back stack and see if your app is still on it?
portfoliobuilder
@Chris.Jenkins that is incorrect. The overridePendingTransition always goes LAST. When calling an intent overridePendingTransition goes last and even in the above example the overridePendingTransition goes last. The effect does work. I wanted to clear this up for anyone who may have gotten the wrong information from Chris' comment
Figured it out. I wasn't finshing the current activity. The following code does the trick.
@Override
public void onBackPressed() {
[This Activity].this.finish();
overridePendingTransition(R.anim.fade_in, R.anim.fade_out);
}
1 Comment
Himanshu Virmani
super.onBackPressed() automatically calls finish. Use that instead of calling finish manually..
if you want no animation
follow the code in Activity
@Override
public void onBackPressed() {
super.onBackPressed();
overridePendingTransition(0,0);
}
Reference : https://developer.android.com/reference/android/app/Activity.html#overridePendingTransition(int, int)
Comments
I wouldn't use onBackPressed() since it's a hack when we use Fragments and we need to handle the stack, for instance. I proposed a more elegant solution here: