Master Chief
Master Chief

Reputation: 2541

loading views in asynctask

I have many views in my app, which takes lot of time during startup. I am using emulator and it takes something like 15 seconds to shows the screen. App runs fine after that.

I tried to implement asynctask to do view loading, but I am getting errors. Android documentation states I should not setup views outside UI thread. But it doesn't say, I can't.

How can I implement a loading screen, which load views. Dropping views is not an option, as app is drawing application and requires those views for quick tool selection.

Or should I setup all my views in onPostExecute(), before calling dialog dismiss. But this will still freeze the UI thread for that time. And I will be in same position.

I haven't tested in device, but I know if UI thread is blocked for more than 5 seconds, it shows app not responding dialog. I want to prevent it.

I am targeting SDK 10. Also, will this take same time when it runs on actual device. App is for Samsung galaxy note, which is quite snappy.

P.S. My internet is still down from 3 days. I am using my mobile client and refferring to offline SDK docs.

Upvotes: 1

Views: 987

Answers (1)

Thomas Dignan
Thomas Dignan

Reputation: 7102

Straight from the docs:

"You must always be on the UI thread when calling any method on any view. If you are doing work on other threads and want to update the state of a view from that thread, you should use a Handler."

Your view logic has to be on the UI thread.

http://developer.android.com/reference/android/view/View.html

As for a "loading" screen:

Try to separate out logic that is not view related and do it in a background thread if you can. You can update a progress bar with AsyncTask. Override onProgressUpdate() and call publishProgress() from your doInBackground() when you want to update progress. Or you can just use indeterminate progress. Up to you.

Upvotes: 1

Related Questions