How can I make a true splash screen in Android? I don\'t want timers or delays. Just a splash screen that is shown until your application has loaded.
The best way to do a splash screen is:
I arrived in this good solution:
import android.app.Activity; import android.content.Intent; import android.os.AsyncTask; import android.os.Bundle; import android.view.WindowManager; import br.eti.fml.android.sigame.R;
import java.util.concurrent.atomic.AtomicBoolean;
public class LauncherActivity extends Activity {
private AsyncTask goingToNextScreen;
private AtomicBoolean alreadyShown = new AtomicBoolean(false);
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.launcher);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
//noinspection unchecked
goingToNextScreen = new AsyncTask() {
@Override
protected Object doInBackground(Object... objects) {
try {
Thread.sleep(1500);
} catch (InterruptedException e) {
// ignores
}
return null;
}
@Override
protected void onPostExecute(Object o) {
goNext();
}
}.execute();
}
@Override
public void onBackPressed() {
if (goingToNextScreen != null) {
goingToNextScreen.cancel(true);
goNext();
}
}
private void goNext() {
if (alreadyShown.compareAndSet(false, true)) {
startActivity(new Intent(LauncherActivity.this, HomeActivity.class));
overridePendingTransition(0, 0);
finish();
overridePendingTransition(0, 0);
}
} }