简体   繁体   中英

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

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

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

Try using this:

SplashScreen.this.finish();

where SplashScreen is the name of the Activity.

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

}

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM