Hello I would like to display an other splashscreen after the default one, if the App starts for the first time(right after installation e.g.)
So I wrote this. But the new Activity does not start it stays at the Splash screen. Can somebody say whats wrong with it?
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.widget.TextView;
public class splash extends Activity {
private Thread splashTread;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
if(!prefs.getBoolean("firstTime", false)) {
// run your one time code
Intent i = new Intent(splash.this, main.class);
startActivity(i);
SharedPreferences.Editor editor = prefs.edit();
editor.putBoolean("firstTime", true);
editor.commit();
// thread for displaying the SplashScreen
splashTread = new Thread() {
@Override
public void run() {
try {
synchronized(this){
//wait 2 sec
wait(2000);
}
} catch(InterruptedException e) {}
finally {
finish();
//start a new activity
Intent i = new Intent();
i.setClass(splash.this, main.class);
startActivity(i);
stop();
}
}
};
splashTread.start();
}
}
}
Thanks.
From what I’ve seen, your code runs the same Activity (main) regardless of whether the launch is the first time or not. I’m assuming your purpose is to immediately launch the alternate splash screen if it’s the first launch, otherwise to proceed to the main Activity after 2 seconds. Also, I would recommend using a Handler rather than a Thread, since you’re only making use of it once, and on a delay. Try this: