Telmo Vaz
Telmo Vaz

Reputation: 199

Splash screen not disappearing

I'm trying to create a thread that is a splash screen that appears while the rest of my app is loading, but for some reason my splash activity does not disappear after 2 seconds as it should. Why is that?

Here is my Splash activity class:

imports ...

public class Splash extends Activity implements Runnable {

    @Override
    protected void onCreate(Bundle tokenArg) {
        super.onCreate(tokenArg);
        setContentView(R.layout.splash);

        Thread splashing = new Thread();
        splashing.start();

    }

    @Override
    public void run() {
        try {
            Thread.sleep(2000);
            startActivity(new Intent(Splash.this, Home.class));
        }
        catch(Exception excpt) {
            AlertDialog alert = new AlertDialog.Builder(this).create();
            alert.setTitle("Error");
            alert.setMessage("App is going to close");
        }
        finally {
            this.finish();
        }
    }
}

And this is the .Home activity class:

public class Home extends Activity {

    @Override
    protected void onCreate(Bundle tokenArg) {
        super.onCreate(tokenArg);
        setContentView(R.layout.activity_home);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.home, menu);
        return true;
    }
}

Both have it's corresponding xml and it is all good with them. (I've tested them individually)

Thanks in advance for your time.

Upvotes: 0

Views: 854

Answers (5)

Sachin Sharma
Sachin Sharma

Reputation: 1

you can try this simple code..

@Override

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    *// activity_splay is the name of splash .xml file*
     setContentView(R.layout.activity_splash);
    Thread thread = new Thread(){
        @Override
        public void run(){
            try{
                // splash screen would disappear after 1500 ms i.e 1.5 sec
                sleep(1500); <br/>
         // MainActivity is the name of activity that would appear after splash screen 
         startActivity(new Intent(getApplicationContext(), MainActivity.class));
            }
            catch (InterruptedException e){
                e.printStackTrace();
            }
        }
    };
    thread.start();
}
//hopefully it would help:)

Upvotes: 0

APerson
APerson

Reputation: 712

It looks like you haven't passed in a class for new Thread();. You should try changing that to new Thread(this);.

Upvotes: 0

Aditya Kamath
Aditya Kamath

Reputation: 2237

While instantiating the thread you have not passed the runnable as a parameter to the Thread constructor. As you have implemented the interface Runnable in Splash.class pass the current object as parameter.

Thread splashing = new Thread(this);
splashing.start();

Hope this helped.

Upvotes: 2

Nitin Sethi
Nitin Sethi

Reputation: 1336

Try this code. It handles quite a lot of use cases and would handle user's back button and a lot of other use cases well.

public class SplashBranding extends Activity {

    private Handler mHandler;
    private static final long TWO_SECOND_IN_MS = 2000; 

    @Override
    protected void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        mHandler = new ShowHomeHandler();   
        setContentView(R.layout.splash);
    }

    @Override
    protected void onResume() {
        super.onResume();
        mHandler.sendEmptyMessageDelayed(0, TWO_SECOND_IN_MS);
    }

    @Override
    protected void onPause() {
        super.onPause();
        // In case system dialogs appear and this method is called, we shouldn't show Home. 
        if(!isFinishing())
            mHandler.removeMessages(0);
    }

    @Override
    protected void onStop() {
        super.onStop();
        // In case a call is received and this method is called, we shouldn't show Home.
        if(!isFinishing())
            mHandler.removeMessages(0);
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        // In case user pressed back, we shouldn't show Home.
        mHandler.removeMessages(0);
    }

    /**
     * Shows the Home Activity.
     */
    private void showHome() {
        Intent i = new Intent(this, Home.class);
        startActivity(i);
        finish();
    }

    private class ShowHomeHandler extends Handler {

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);

            switch (msg.what) {
            case 0:
                showHome();
                break;
            default:
                break;
            }
        }
    }

}

Upvotes: 0

Tarun
Tarun

Reputation: 13808

Instaed of a thread use this code to launch your activity.

new Handler().postDelayed(new Runnable(){
                public void run() {
                    Intent mainIntent = new Intent(Splash.this, Home.class)
                            .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
                            .addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
                    Splash.this.startActivity(mainIntent);

                    Splash.this.finish();
                }
            }, 2000); 

Upvotes: 1

Related Questions