Tell me more ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

Is this code I found on the web overly complicated?

final int welcomeScreenDisplay = 4000;
/** create a thread to show splash up to splash time */
Thread welcomeThread = new Thread() {

    int wait = 0;

    @Override
    public void run() {
        try {
            super.run();
            while (wait < welcomeScreenDisplay) {
                sleep(100);
                wait += 100;
            }
        } catch (Exception e) {
            Log.v(TAG, "EXc=" + e);
        } finally {
            Intent i = new Intent(getBaseContext(), HomeScreen.class);
            startActivity(i);
            finish();
        }
    }
};
welcomeThread.start();

All I am looking to do is wait 4 seconds before staring the presenting a home screen.

Thanks

share|improve this question

2 Answers

up vote 2 down vote accepted

Why not

final int welcomeScreenDisplay = 4000;
/** create a thread to show splash up to splash time */
Thread welcomeThread = new Thread() {
    @Override
    public void run() {
        try {

This does not make sense. Why not directly sleep for the full amount?

                sleep(welcomeScreenDisplay);

You want to use the other logic if you were looking for some event like a touch periodically.

        } catch (InterruptedException e) {
        } finally {
            finish();
            startActivity(new Intent(getBaseContext(), HomeScreen.class));
        }
    }
};
welcomeThread.start();
share|improve this answer

A handler might be used to implement this :-

public class SplashActivity extends Activity {
    private Handler handler = new Handler();
    private final int welcomeScreenDisplay = 4000;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);
        handler.postDelayed(new Runnable() {

            @Override
            public void run() {
                Intent i = new Intent(getBaseContext(), HomeScreen.class);
                startActivity(i);
                finish();
            }
        }, welcomeScreenDisplay);
    }
}

where R.layout.activity_splash contains the UI for splash screen

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.