Android - How to animate an activity transition when the default back button is pressed
Solution 1
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);
}
Solution 2
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);
}
Solution 3
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.
Solution 4
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>
Related videos on Youtube
Bitcoin Cash - ADA enthusiast
Bitcoin Cash donations: bitcoincash:qpzkuqj5j0f4f6h5q7j2qnzuwlj0prat8c8xek3ndn ADA donations: addr1q94a6fs54ktss7k8yezsd9y3w6f7jlz2wa9zjxe7zlh869636m56ftcgfwp5ernjfu422dh3869hhnksyvn9dsla4gjqzl92xe
Updated on May 02, 2020Comments
-
Bitcoin Cash - ADA enthusiast about 4 years
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?
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.
Thank you in advance!
-
TaoZang over 11 yearswhen you press the default back button, the activity will call the onBackPressed method. And if you have overrided this method, the activity won't be finished so you should call finish() yourself.
-
Dallas about 10 yearsActually, you should call super.onBackPressed(), not assume the functionality.
-
zionpi almost 9 yearsHow to do it for globally all activies?
-
TaoZang almost 9 yearsDo this in your BaseActivity class, and extends all your custom Activity classes from the BaseActivity class. @zionpi
-
tir38 almost 8 yearsCan you add anything to @TaoZang 's answer?
-
YakovL over 7 yearsWhile the answer may be helpful, I'd recommend to add some annotation to it. As currently written, it is an amount of code without any highlightened parts (where one should look to find the key parts).
-
VorteXavier almost 7 yearsIn my case, this solution was the way to go. The other solutions didn't work in every scenario for my application, because there are certain cases where a I call finish() without triggering onBackPressed()
-
cesards almost 7 yearsExactly :-) Glad it was helpful