Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

overridePendingTransition() isn't working

I am trying to implement a quick SplashActivity that slides in from the right to the left, then slides out to the left as the MainActivity slides in to the right. Currently, the animation XMLs are in place but it isn't working, the splash screen just appears, then the transition to the main activiity doesn't work either. Here is the SplashActivity that is the beginning activity of the app.

public class SplashActivity extends Activity{

    private int SPLASH_MILLISECONDS = 1000;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        overridePendingTransition(R.anim.slide_in_left, R.anim.slide_out_right);
        setContentView(R.layout.activity_splash);
        runTimer();
    }

    private void runTimer() {
        Timer timeout = new Timer();
        timeout.schedule(new TimerTask()
        {
            @Override
            public void run() 
            {                       
                runOnUiThread(new Runnable(){
                    @Override
                    public void run() {
                        goToMainActivity();
                    }
                });
            }                           
        }, SPLASH_MILLISECONDS);
    }

    private void goToMainActivity(){
        Intent intent = new Intent(this, MainActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION );
        startActivity(intent);
        finish();
        overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_left);    
    }
}

This for some reason does not work, what is it I am doing wrong?

EDIT: This is my new goToMainActivity() method, yet still neither of the 2 animations are working. At this point I don't care much about the first animation working, I just want the second one working.

private void goToMainActivity(){
        Intent intent = new Intent(this, MainActivity.class);
        startActivity(intent);
        overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_left);    
        finish();
    }
like image 939
AggieDev Avatar asked Jun 27 '14 22:06

AggieDev


2 Answers

i typically override the animation from within the onCreate() function of the activity being started, and this seems to work well. the caller should just startActivity() normally; don't use overridePendingTransition() or FLAG_ACTIVITY_NO_ANIMATION or anything else.

copied & pasted from actual deployed code:

@Override public void onCreate(Bundle savedState)
{
    overridePendingTransition(R.anim.anim_slideup, R.anim.anim_hold);
    super.onCreate(savedState);

as someone else mentioned, when you override the animation an activity starts with, you should also override the animation that activity finishes with to match.

@Override public void finish()
{
    super.finish();
    overridePendingTransition(R.anim.anim_hold, R.anim.anim_slidedown);
}
like image 126
j__m Avatar answered Nov 03 '22 11:11

j__m


A possible trick to get a custom transition for the first activity is to not make it the actual first activity. You can set up a transparent activity as the first one, and having it immediately call the splash screen. Since the first activity is invisible, the perceptible effect is that the splash will enter the screen with the animation you desire.

While transparent activities are not recommended for performance reasons (since they cause both it and the previous activity to be drawn for each frame) this activity will last for a very short time (just enough to be drawn and call the splash one) so it doesn't matter.

For example:

In AndroidManifest.xml:

    <activity
        android:name=".LauncherTransparentActivity"
        android:label="@string/app_name"
        android:theme="@android:style/Theme.Translucent" 

        >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <activity android:name=".SplashActivity" android:label="@string/app_name" />
    <activity android:name=".MainActivity" android:label="@string/app_name" />

Then this code in LauncherTransparentActivity.java:

@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    getWindow().requestFeature(Window.FEATURE_NO_TITLE);

    Handler handler = new Handler();
    handler.postDelayed(new Runnable()
    {
        @Override
        public void run()
        {
            Intent intent = new Intent(LauncherTransparentActivity.this, SplashActivity.class);
            startActivity(intent);
            overridePendingTransition(R.anim.slide_in_left, R.anim.slide_out_left);
            finish();
        }

    }, 100);

And finally, in SplashActivity.onCreate(), practically the same code, but with a longer delay and calling the actual MainActivity (more or less the same code as you had).

Two caveats:

  1. Whenever you call overridePendingTransition() to start an activity, be sure to call it again whenever it's finished, with a transition that is logically the opposite. Otherwise it will use the system-default animation when exiting, which is not what you would logically expect (e.g. if it enters from the right, it should probable exit the same way).
  2. Although this is possible (as demonstrated), it may not be a very good idea to override the system default transition for application launch. Users expect certain behavior when launching an app -- basically one that is consistent with launching any other app. Using a custom one instead may be confusing.
like image 1
matiash Avatar answered Nov 03 '22 11:11

matiash