Nibha Jain
Nibha Jain

Reputation: 8161

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();

Upvotes: 0

Views: 1824

Answers (5)

kodemetrics
kodemetrics

Reputation: 76

public class SplashActivity extends Activity {

    Handler handler;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash_screen);

        handler=new Handler();
        handler.postDelayed(() -> {
            Intent intent=new Intent(SplashActivity.this, Home.class);
            startActivity(intent);
            finish();
        },3000);
    }
}

AndriodManifest.xml

    <activity
        android:name=".SplashActivity"
        android:theme="@style/AppTheme"
        android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

Upvotes: 0

Piyush
Piyush

Reputation: 548

Try using this:

SplashScreen.this.finish();

where SplashScreen is the name of the Activity.

Upvotes: 0

HarmenH
HarmenH

Reputation: 221

this solution only solves the problem for the back-button. If users press the home-button the unwanted behavior will still occur. Wouldn't it be easier to overwrite the onStop method and do your thing in there?

@Override
public void onStop(){
    super.onStop();
    if(splashTread.isAlive())
       this.stop = true;
}

Upvotes: 0

Sujit
Sujit

Reputation: 10632

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;
}

}

Upvotes: 0

Hein
Hein

Reputation: 2683

It's because you call finish(); before the startActivity(new Intent("next activity"));

Swap finish(); with startActivity(new Intent("next activity"));

Upvotes: 2

Related Questions