1
\$\begingroup\$

I have implemented a splash screen which is the main activity of my Android app. This splash screen shows an image, and after 1000ms there is an animation to show another activity's layout.

The code I've written is the following:

public class SplashScreen extends AppCompatActivity {

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.splash_screen);
        startTheTransitionAfterTheSplashScreen();
    }

    private void startTheTransitionAfterTheSplashScreen() {
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                Intent intentSplashScreenToActivityJustAfterSplashScreen = new Intent(SplashScreen.this, ActivityJustAfterSplashScreen.class);
                startActivity(intentSplashScreenToActivityJustAfterSplashScreen);
                overridePendingTransition(R.anim.animation_enter_activity, R.anim.animation_leave_activity);
                finish();
            }
        }, 1000);
    }
}

And for the animations:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:shareInterpolator="false">
    <translate
        android:fromXDelta="100%" android:toXDelta="0%"
        android:fromYDelta="0%" android:toYDelta="0%"
        android:duration="500" />
</set>

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:shareInterpolator="false">
    <translate
        android:fromXDelta="0%" android:toXDelta="-100%"
        android:fromYDelta="0%" android:toYDelta="0%"
        android:duration="500" />
</set>

Question

What I would want to ask is especially about the startTheTransitionAfterTheSplashScreen (I think the animations are correctly written): should I actually use a Runnable with a Handler to delay the transition? (in other words: is it suitable for this splash screen, or is there a better way to do that?)

Edit

Implementing K.H.'s answer:

public class SplashScreen extends AppCompatActivity {

    private Handler the_transition_handler;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.splash_screen);
    }

    @Override
    protected void onStart() {
        super.onStart();
        startTheTransitionAfterTheSplashScreen();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        the_transition_handler.removeCallbacksAndMessages(null);
    }

    private void startTheTransitionAfterTheSplashScreen() {
        the_transition_handler = new Handler();
        the_transition_handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                final Intent intentSplashScreenToActivityJustAfterSplashScreen = new Intent(SplashScreen.this, ActivityJustAfterSplashScreen.class);
                startActivity(intentSplashScreenToActivityJustAfterSplashScreen);
                overridePendingTransition(R.anim.animation_enter_activity, R.anim.animation_leave_activity);
                finish();
            }
        }, 1000);
    }
}
\$\endgroup\$

1 Answer 1

2
\$\begingroup\$

I don't like name startTheTransitionAfterTheSplashScreen. Too long, you are already in SplashScreen. Maybe startTransition or startTransitionafter1second.

Using handler is fine, but what if user manages to change orientation during that 1 second? Activity gets destroyed and recreated. Now you have 2 active handlers and I expect ActivityJustAfterSplashScreen will be called 2 times.

I suggest keeping reference to handler so that you can cancel in onDestroy - handler.removeCallbacksAndMessages(null).

\$\endgroup\$
3
  • \$\begingroup\$ Thank you! I have edited my code to show you the modification. Do you agree with them? \$\endgroup\$ Dec 28, 2019 at 14:17
  • 1
    \$\begingroup\$ Yep, just use camelcase for class property the_transition_handler. \$\endgroup\$
    – K.H.
    Dec 28, 2019 at 14:25
  • \$\begingroup\$ Android Studio doesn't use camelcase :-/ \$\endgroup\$ Dec 28, 2019 at 14:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.