Search code examples
androidmultithreadingperformanceandroid-layoutsplash-screen

What is the preferred way for splash screen? - using handler or creating new thread?


I am making a splash screen by following this tutorial.

Here, the poster have mentioned two ways for creating splash screen.

METHOD 1: Create a thread and set time to sleep after that redirect to main app screen.

  Thread background = new Thread() {
            public void run() {

                try {
                    // Thread will sleep for 5 seconds
                    sleep(5*1000);

                    // After 5 seconds redirect to another intent
                    Intent i=new Intent(getBaseContext(),FirstScreen.class);
                    startActivity(i);

                    //Remove activity
                    finish();

                } catch (Exception e) {

                }
            }
        };

        // start thread
        background.start();

METHOD 2: Set time to handler and call Handler().postDelayed , it will call run method of runnable after set time and redirect to main app.

 new Handler().postDelayed(new Runnable() {

        // Using handler with postDelayed called runnable run method

        @Override
        public void run() {
            Intent i = new Intent(MainSplashScreen.this, FirstScreen.class);
            startActivity(i);

            // close this activity
            finish();
        }
    }, 5*1000);

I searched over the way abut could not found what is the difference between these two approaches.

Can anyone say me which is the preferred way in the context of use of resources and memory?


Solution

  • Technically METHOD 2 (using handler) is the way to go. Method 1 calls UI stuff on a background thread, which may lead to bad results.