Is it a bad practice use Thread.sleep(Milliseconds) for wait a bit before start another activity?

android, java, thread-sleep

Solution

Sleeping on the UI thread is always a bad idea. In this case you are in an `onPostExecute`, which is on the UI thread.

Throw your sleep into the `doInBackground` method of your `AsyncTask` instead, and you won't get any ANR's there (Android not responding).

Users don't like waiting for splash screens, so it is better not to wait at all. But sometimes the splash screen is required (ie due to contracts).

Problem

I'm making a SplashScreen for an app ... When the app starts, it start LoadingActivity ... sleep for 3 seconds, finish(); and then starts the MainActivity. Splash serves to update the database. If the database is already updated, I want the splash still for 3 seconds anyway. I am using the following code: ``` protected void onPostExecute(Void result) { super.onPostExecute(result); try { Thread.sleep(3000); } catch (InterruptedException e) { e.printStackTrace(); } finally { Intent intent = new Intent(LoadingActivity.this, MainActivity.class); startActivity(intent); finish(); } } ``` Is it a bad pratice? and why? The app is running nicelly in AVD.

Original source