116

I have two Activities A and B. I want to have the shrink Animation when Activity A calls B and maximize animation when Activity B calls A. I don't need the animation xml files for this.

When we call another Activity in Android it gives its default animation and then it calls shrink animation.

What I want is that the default animation should not occur and the animation that I want should occur.

Can we actually give the animation when calling another Activity?

Alex Lockwood
  • 83,063
  • 39
  • 206
  • 250
sunil
  • 9,541
  • 18
  • 66
  • 88

3 Answers3

152

Since API 16 you can supply an activity options bundle when calling Context.startActivity(Intent, Bundle) or related methods. It is created via the ActivityOptions builder:

Intent myIntent = new Intent(context, MyActivity.class);
ActivityOptions options = 
   ActivityOptions.makeCustomAnimation(context, R.anim.fade_in, R.anim.fade_out);
context.startActivity(myIntent, options.toBundle());

Don't forget to check out the other methods of the ActivityOptions builder and the ActivityOptionsCompat if you are using the Support Library.



API 5+:

For apps targeting API level 5+ there is the Activities overridePendingTransition method. It takes two resource IDs for the incoming and outgoing animations. An id of 0 will disable the animations. Call this immediately after the startActivity call.

i.e.:

startActivity(new Intent(this, MyActivity.class));
overridePendingTransition(R.anim.fade_in, R.anim.fade_out);

API 3+:

You can prevent the default animation (Slide in from the right) with the Intent.FLAG_ACTIVITY_NO_ANIMATION flag in your intent.

i.e.:

Intent myIntent = new Intent(context, MyActivity.class);
myIntent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
context.startActivity(myIntent);

then in your Activity you simply have to specify your own animation.

This also works for the 1.5 API (Level 3).

whlk
  • 15,487
  • 13
  • 66
  • 96
78

You must use OverridePendingTransition method to achieve it, which is in the Activity class. Sample Animations in the apidemos example's res/anim folder. Check it. More than check the demo in ApiDemos/App/Activity/animation.

Example:

@Override
public void onResume(){
    // TODO LC: preliminary support for views transitions
    this.overridePendingTransition(R.anim.in_from_right, R.anim.out_to_left);
}
Gian Segato
  • 2,359
  • 3
  • 29
  • 37
Praveen
  • 90,477
  • 74
  • 177
  • 219
  • 1
    Thanks for the reply. The method overridePendingTransition is supported from API level 5 and I want the application to be supported from Android 1.5. Is there any other way of providing Animation from one Activity to other. – sunil Apr 17 '10 at 06:21
  • 5
    awesome! this is a lot of fun to play with – Someone Somewhere May 25 '11 at 21:47
  • @sunil i have same quetion like yours.could you solve the problem?animation with api level 3 or 4 ( not 5 ) – Dr.jacky Oct 23 '12 at 07:19
  • keeping animation code in onResume() will cause the animation to play when the user goes outside the app and comes back. Not ideal in many apps. – Vihaan Verma Jul 15 '15 at 11:27
  • I needed to add `super.onResume();`, was getting: *"android.util.SuperNotCalledException: Activity {com.abc.MyActivity} did not call through to super.onResume()"* – Gene Bo Nov 09 '16 at 19:45
3

Jelly Bean adds support for this with the ActivityOptions.makeCustomAnimation() method. Of course, since it's only on Jelly Bean, it's pretty much worthless for practical purposes.

phreakhead
  • 14,721
  • 5
  • 39
  • 40