Stack Exchange Network
Stack Exchange network consists of 183 Q&A communities including
Stack Overflow
, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.
Visit Stack Exchange
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It only takes a minute to sign up.
Sign up to join this community
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Learn more about Teams
Ask Question
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" />
<?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" />
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?)
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);
\$\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)
.
\$\begingroup\$
\$\endgroup\$
–
\$\begingroup\$
\$\endgroup\$
–
Thanks for contributing an answer to Code Review Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
Use MathJax to format equations. MathJax reference.
To learn more, see our tips on writing great answers.