Search code examples
androidsplash-screen

Splash screen issue


I am creating a splash screen using the following code ,when i press back key the application moves to the home screen and within a few seconds shows my next mainmenu screen.I am calling finish() in onBackPressed(),I want to close the app on pressing back key in the splash screen.can any one help me on this??

Thanks!!

     Thread splashThread = new Thread() {
        @Override
        public void run() {
           try {
              int waited = 0;
              while (_active && (waited < 2000)) {
                 sleep(100);
                 if(_active) {
                     waited += 100;
                 }
              }
           } catch (InterruptedException e) {
              // do nothing
           } finally {

               finish();
               startActivity(new Intent("next activity"));
               stop();
           }
        }
     };
     splashThread.start();

Solution

  • It is working in my application

    public class Splash extends Activity {
    protected boolean _active = true;
    protected int _splashTime = 3000;
    Thread splashTread;
    private boolean stop = false;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.splash);
    
        splashTread = new Thread() {
            @Override
            public void run() {
                try {
                    int waited = 0;
                    while(_active && (waited < _splashTime)) {
                        sleep(100);
                        if(_active) {
                            waited += 100;
                        }
                    }
    
                } catch(InterruptedException e) {
                    // do nothing
                } finally {
    
                    if(!stop){
                        startActivity(new Intent(Splash.this,Home.class));
                        finish();
                    }
                    else
                        finish();
                }
            }
    
        };
        splashTread.start();
    
    }
    
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if ((keyCode == KeyEvent.KEYCODE_BACK)) {
    
            if(splashTread.isAlive())
                this.stop = true;
        }
        return true;
    }
    

    }