2

Given a one-activity app like this:

public class MyActivity extends Activity {
  protected void onCreate(Bundle savedInstanceState) {

  loadingScreen = new RelativeLayout(this);
  // add a bitmap to loadingScreen
  setContentView(loadingScreen);

  Dict dict = new Dict();
  dict.init(); 
  // this method takes ~10 seconds 
  }
}

In theory, it looks like this would display a loading screen while the dictionary initializes. In practice, nothing is displayed until onCreate() returns, so the user sees a blank white screen for 10 seconds. What's the best way to display a please wait while this app loads screen?

I believe I could create a separate thread for dict.init(), but that seems like overkill in this case because I don't want the app to be usable or interactive while dict.init() runs. I'd like it to run on the main thread (and hang the rest of the app while it executes), I just want to display something on the screen first.

PS, I tried moving dict.init() to onStart(), that appeared to have no effect.

edit: Perhaps I should have clarified this to avoid getting "You're doing it wrong" type answers, but the init takes 2 or 3 seconds on modern phones and tables, 10 seconds is a worst-case on old phones. This app is a word game, it can't be used without the dictionary. Moving dict.init() to an async task will not improve the user's experience, and the question I asked is whether it's possible to display a splash screen without doing that. I gather that the answer is "No."

DevOfZot
  • 1,362
  • 1
  • 13
  • 26
  • 1
    Check this [link](http://stackoverflow.com/questions/5486789/how-do-i-make-a-splash-screen-in-android) – Lal May 05 '14 at 18:15
  • 1
    *Don't* run something that takes 10s in `onCreate()`. At best, your user will wonder what went wrong and discontinue the use of your app, at worst, you'll get an ANR and/or your app will be closed. Offload long-running task into `AsyncTask`. – 323go May 05 '14 at 18:17
  • You need to create AsyncTask to parse your dict, meanwhile you can show progress bar from same.Check this - http://developer.android.com/reference/android/os/AsyncTask.html – Kanak Sony May 05 '14 at 18:18
  • ANRs occur after as little as 5s – Phantômaxx May 05 '14 at 18:23

1 Answers1

3

I'd like it to run on the main thread (and hang the rest of the app while it executes)

No, no wouldn't and your users wouldn't like you much either

I believe I could create a separate thread for dict.init(), but that seems like overkill in this case because I don't want the app to be usable or interactive while dict.init() runs.

This is exactly what you should do. You can use a variety of ways including an AsyncTask and show a ProgressDialog while the work is being done.

so the user sees a blank white screen for 10 seconds.

If it is taking this long then you might want to rethink your flow. Even if you go with a separate Thread and show a ProgressBar, most users aren't going to want to stare at that for 10 seconds. You should load the data in the background and allow them to do something else while it loads, if possible. You could use something like an IntentService depending on how you are getting the data.

Example of AsyncTask

Painless Threading

Community
  • 1
  • 1
codeMagic
  • 44,549
  • 13
  • 77
  • 93
  • I edited to clarify, but this app is not usable until the dictionary is parsed, so moving it to an asynctask will not improve the user experience. However, it sounds like what I wanted to do is not possible, and the answer to my question is that I have to use a separate activity (as in the link in Lal's comment) or an async task (as in this answer) or both. – DevOfZot May 05 '14 at 18:38
  • `AsyncTask` will improve the experience because it will allow you to do the heavy work in a background `Thread` while showing a `ProgressDialog`. But, yes, a splash screen would also work – codeMagic May 05 '14 at 18:41