waa1990 waa1990 - 2 months ago 13
Java Question

how do i show a splash screen only when the activity starts up not when it is resumed?

i want the spash screen to only show when the app has been compltely destroyed not when it is running in the background and resumed

Answer

A very simple method:

Main Activity is only a splash screen. This Activity is shown while a timer starts that elapses for say 4 seconds.

When 4 seconds hits, the splash screen activity is destroyed and the Main Application Activity is started.

Voila, you now have a splash screen that will never be shown, except when you first start the application.

public class SplashScreen extends Activity {

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

        Thread t = new Thread() {
            public void run() {
                try {
                    int time = 0;
                    while (time < 4000) {
                       sleep(100);
                       time += 100;
                    }
                }
                catch (InterruptedException e) {
                    // do nothing
                }
                finally {
                    finish();
                    Intent i = new Intent(SplashScreen.this, MainApplication.class);
                    startActivity(i);
                }
            }
        };
        t.start();

    }
}
Comments