2

I want to create an activity that opens when I start my app, wait some time and jumps to the next activity without the user pressing anything.

Here is my code:

public class MainActivity extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Thread thread = new Thread();
    thread.start();
}

public class waitSeconds extends Thread {

    public void run() {
        Log.i("MyActivity", "MyClass");
        try {
            wait(300);
            Intent intent = new Intent(MainActivity.this, main_window.class);
            startActivity(intent);

        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

As it seems that it is never going to the "run" method.

How can I do this?

halfer
  • 19,824
  • 17
  • 99
  • 186
roiberg
  • 13,629
  • 12
  • 60
  • 91
  • You would have to call `Thread thread = new waitSeconds();` in order for your `waitSeconds` class to ever be used. – Nate Jul 16 '12 at 08:30

2 Answers2

1

include this in your Activity:

 public class MainActivity extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    SplashHandler handler=new SplashHandler();
    Message msg = new Message();
    msg.what = 0;
    handler.sendMessageDelayed(msg, 3000);

}

private class SplashHandler extends Handler {

            public void handleMessage(Message msg)
              {
                switch (msg.what)
                {
                default:
                case 0:
                  super.handleMessage(msg);

                  Intent intent = new Intent(MainActivity.this,main_window.class);
                  startActivity(intent);
                  MainActivity.this.finish();
                }
              }
        }
AkashG
  • 7,868
  • 3
  • 28
  • 43
  • Thanks! its working great! but its saying "This Handler class should be static or leaks might occur" how can i fix this?\ – roiberg Jul 16 '12 at 08:05
  • Implement this as your MainActivity – AkashG Jul 16 '12 at 08:20
  • thats exactly what i did. still the same problem. – roiberg Jul 16 '12 at 08:30
  • It does work fine for the most part, but their is a reason it's warning you. I had the same problem and eventually it created a "memory leak". See this answer on how to properly implement a static handler, it's pretty easy and will save headaches later on. http://stackoverflow.com/questions/11407943/this-handler-class-should-be-static-or-leaks-might-occur-incominghandler – Zachary Moshansky Nov 20 '12 at 00:55
1

you need something like this:

public class SplashScreenActivity extends Activity {

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.splash);

        new Thread() {

            public void run() {

                try {
                    Intent i = new Intent(SplashScreenActivity.this,
                            MainActivity.class);

                    Thread.sleep(2000);

                    startActivity(i);

                    finish();

                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }.start();

    }

}
Nate
  • 31,017
  • 13
  • 83
  • 207
user1508284
  • 71
  • 1
  • 5