Disabling Activity “slide” animation on StartActivity, Finish() and BackPressed

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: , , ,

4 responses

  1. Posted March 7, 2011 at 17:59 | Permalink

    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);
    }

  2. Posted October 17, 2011 at 23:03 | Permalink

    Thanks! Works like a charm ;)

  3. Yannick
    Posted March 20, 2012 at 15:18 | Permalink

    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()

  4. Sam
    Posted March 28, 2012 at 06:24 | Permalink

    Awesome! Works great!

Leave a Reply