This post is about Android development, because I’m currently doing both. Hopefully this year I’ll have enough time to update my blog more frequently. Anyway, back to programming: how to disable “slide” animation on various Activity related events.
Disable animation on StartActivity
Before starting your activity, you have to set flag FLAG_ACTIVITY_NO_ANIMATION to your Intent.
Intent myIntent = new Intent(); myIntent.setClassName(this, ExampleActivity.class.getName()); myIntent.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION); startActivity(myIntent);
Unfortunately, this will not disable animations caused by back button or, if you’ve started SubActivity, on finish().
Disable animation on finish() and back button pressed
After your finish() function put overridePendingTransition(0, 0):
Intent resultIntent = new Intent(); setResult(Activity.RESULT_OK, resultIntent); finish(); overridePendingTransition(0, 0);
Same function helps with handling back button animation. Just override onBackPressed in your activity
@Override
public void onBackPressed() {
this.finish();
overridePendingTransition(0, 0);
}
Tags: FLAG_ACTIVITY_NO_ANIMATION, onBackPressed, overridePendingTransition, StartActivity
I have noticed that the onBackPressed() implementation does not always work. For me I still get a swipe for TabActivities.
To overcome this I put the older (legacy) approach in the onCreate method of the Activity that I DON’T want to animate:
public void onCreate(Bundle savedInstanceState) {
…
// Disable all animations
getWindow().setWindowAnimations(0);
}
Thanks! Works like a charm
Thanks.
Instead of adding overridePendingTransition(0, 0) after finish() and overriding onBackPressed(), it’s probably easier to override finish() itself:
@Override
public void finish() {
super.finish();
overridePendingTransition(0, 0);
}
Like this, if later you add another way to “exit” the activity by calling finish(), you don’t have the risk to forget to add the overridePendingTransition()
Awesome! Works great!