4

I want to apply an activities transition like the one that HTC phones have: When you start a new Activity there is a slide right-to-left, but when you press the back button to return to the previous Activity, there is a slide left-to-right animation.

I have used overridePendingTransition in the onResume of my Activities to simulate the slide right to left animation, but when I press the back button, the same animation is executed which is wrong (from a result perspective).

Thus I would like to ask how to manage having different animations for an Activity, one for when it is created, and one for when the user press back key.

Thanks a lot!

Dimitris Makris
  • 5,183
  • 2
  • 34
  • 54

2 Answers2

8

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

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

So this animation will only be shown when the back button gets pressed.

To set an animation when a new Activity opens, you just have to define the animation after you called startActivity() or similar:

startActivity(some_intent);
overridePendingTransition(R.anim.in_from_right, R.anim.out_to_left);
Floern
  • 33,559
  • 24
  • 104
  • 119
0

you have use default animation in android activity overridePendingTransition

startActivity(new Intent(getApplicationContext(),Login.class));
overridePendingTransition(android.R.anim.fade_in,android.R.anim.fade_out);
milan pithadia
  • 840
  • 11
  • 16