Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You are blocking the UI thread, which is a big no-no. The system cannot paint the screen until your <code>onCreate</code> method returns. A common way to do what you want is to start a separate thread that waits and then posts a Runnable to the UI thread:</p> <pre><code>@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.logo); final Handler handler = new Handler(); final Runnable doNextActivity = new Runnable() { @Override public void run() { // Intent to jump to the next activity Intent intent= new Intent(this, NextActivity.class); startActivity(intent); finish(); // so the splash activity goes away } }; new Thread() { @Override public void run() { SystemClock.sleep(2000); handler.post(doNextActivity); } }.start(); } </code></pre> <p>A somewhat simpler way (as Athmos suggested in his answer) is to let the handler do the countdown for you:</p> <pre><code>Handler mHandler; Runnable mNextActivityCallback; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.logo); mHandler = new Handler(); mNextActivityCallback = new Runnable() { @Override public void run() { // Intent to jump to the next activity Intent intent= new Intent(this, NextActivity.class); startActivity(intent); finish(); // so the splash activity goes away } }; mHandler.postDelayed(mNextActivityCallback, 2000L); } </code></pre> <p>This has the advantage that you can cancel going on to the next activity (if, say, the use presses the Back button or if you detect an error condition or something during those 2 seconds):</p> <pre><code>@Override protected void onPause() { if (isFinishing()) { mHandler.removeCallbacks(mNextActivityCallback); } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload