xyz
xyz

Reputation: 1363

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.

Upvotes: 0

Views: 573

Answers (2)

user3855135
user3855135

Reputation:

Try this,I always use this code in my Splash Activities.

 public class SplashScreen extends Activity
   {
private Thread mSplashThread;

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

    mSplashThread = new Thread()
    {
        @Override
        public void run()
        {
            try
            {
                synchronized (this)
                {
                    wait(2000);
                }
            }
            catch(InterruptedException ex) {
                ex.printStackTrace();
            }
            finish();
            startActivity(new Intent(getApplicationContext(),MainActivity.class));
        }
    };
    mSplashThread.start();
}

@Override
public boolean onTouchEvent(MotionEvent evt)
{
    if (evt.getAction() == MotionEvent.ACTION_DOWN)
    {
        synchronized (mSplashThread)
        {
            mSplashThread.notifyAll();
        }
    }
    return true;
}
    }

Upvotes: 0

ρяσѕρєя K
ρяσѕρєя K

Reputation: 132972

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

            t = new Thread(this);

Upvotes: 1

Related Questions