Search code examples
javaandroidmultithreadingandroid-intentsplash-screen

Splash screen stuck to the screen - intent to the main activity is not starting


I was creating a splash screen in android using Thread.sleep(). (I know the another method - using handler, but I have to use this method for now.)

My code is as follows:

public class SplashScreen extends Activity { 
   Thread t;
             @Override
                protected void onCreate(Bundle savedInstanceState) {
                    super.onCreate(savedInstanceState);
                    setContentView(R.layout.activity_spash_screen);

            new myclass();
        }
            class myclass implements Runnable{

                myclass()
                {
                t = new Thread();
                t.start();
                }

                public void run()
                {
                    try{
                        Thread.sleep(1000);
                    Intent i = new Intent(getApplicationContext(), MainActivity.class);
                       startActivity(i);
                       finish();
                    }
                    catch(InterruptedException e){
                    System.out.println("thread interrupted");

                    }
                    }

                }
            }

It does not show any error, but splash screen stuck to the screen.

After 1s, it did not start another intent.

If you know the mistake then please help me.


Solution

  • run method of runnable is not called because you are not passing runnable to Thread constructor. so pass it as:

                t = new Thread(this);