The answers above is very good, but I would like to add something else. I am new to Android, I met these problem during my development. hope this can help someone like me.
The Splash screen is the entry point of my app, so add the following lines in AndroidManifest.xml.
The splash screen should only show once in the app life cycle, I use a boolean variable to record the state of the splash screen, and only show it on the first time.
public class SplashActivity extends Activity {
private static boolean splashLoaded = false;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (!splashLoaded) {
setContentView(R.layout.activity_splash);
int secondsDelayed = 1;
new Handler().postDelayed(new Runnable() {
public void run() {
startActivity(new Intent(SplashActivity.this, MainActivity.class));
finish();
}
}, secondsDelayed * 500);
splashLoaded = true;
}
else {
Intent goToMainActivity = new Intent(SplashActivity.this, MainActivity.class);
goToMainActivity.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(goToMainActivity);
finish();
}
}
}
happy coding!