Android activity transition animation

I want to apply a transition to actions similar to what HTC phones have. When you start a new action, a slide appears from right to left, but when you press the back button to return to the previous action, the animation of the slides is from left to right.

I used overridePendingTransition in onResume my actions to simulate a slide animation from right to left, but when I click the back button, the same animation is executed, which is incorrect (in terms of the result).

Thus, I would like to ask how to manage different animations for Activity, for those that were created, and for those when the user presses a key.

Thanks a lot!

+4
source share
1 answer

To define an animation when the user clicks the back button, you need to override onBackPressed() in Activity and use overridePendingTransition() there:

 public void onBackPressed() { super.onBackPressed(); overridePendingTransition(R.anim.in_from_left, R.anim.out_to_right); } 

Thus, this animation will be displayed only when you click the "Back" button.

To set up the animation when a new action is opened, you just need to define the animation after calling startActivity() or similar:

 startActivity(some_intent); overridePendingTransition(R.anim.in_from_right, R.anim.out_to_left); 
+7
source

Source: https://habr.com/ru/post/1432226/


All Articles