Start a new activity with no transition animation

2019-03-10 18:50发布

问题:

I am aware that from API level 5 it is possible to specify a flag in the intent to prevent the normal animation being applied when I start a new activity:

myIntent.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);

However, my question is: is there a way to achieve the same thing in an app supporting android 1.6?

回答1:

Use this: getWindow().setWindowAnimations(0); within the Activity that is starting.



回答2:

On the newer versions, you want to override the transition with 0,0 shortly after you start the activity:

Intent i = new Intent(this, YourNewActivity.class); 
startActivity(i);
overridePendingTransition(0,0);

I tried this on 2.1 and 4.0.3, it worked for me. =)

I found it in the docs here



回答3:

This solution worked for me (Android 2.2):

Intent intent = new Intent(getContext(), YourClass.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
getContext().startActivity(intent);


回答4:

FYI, I'm on 4.4.2 and these are the only things I found that work:

To prevent the opening animation: in the launching activity's onCreate(), add overridePendingTransition(0,0).

To prevent the closing animation: after finish(), immediately call overridePendingTransition(0,0).

These DON'T work (at least not on my 4.4.2 build): (1) calling getWindow().setWindowAnimations(0) in onCreate() (passing in a non-zero number DOES work, but that's an ugly hack since it's expecting a resId), and (2) calling overridePendingTransition(0,0) immediately after startActivity() -- animation is still present.



回答5:

put below code on onCreate of Activity:

getWindow().setWindowAnimations(0);