How to display a waiting dialog in android
I have a long task that requires 10 / 15 seconds. I want to display a dialog, or a view or other stuff to let the user know "hei! I'm working for you. I'm at 90%".
Actually I have 3 layouts: the currentLayout, the progressLayout and the resultLayout. From the main activity I have a function like that:
...
setContentView(R.layout.progressLayout );
SomeLongTask();
setContentView(R.layout.resultLayout);
...
but the progressLayout never shows.
What's the best way to开发者_运维问答 do this?
You are starting the long Task in your User Interface Thread. This thread is also responsible for redrawing the UI and acting on Input Events. If you make a blocking call to the long task function your UI Thread will be busy to do the task and the User Interface can not change. The Android System even will think your Application crashed because it is not reacting to user input anymore.
You have to start the task in an AsyncTask and do all the heavy work in the doInBackground method and update your UI in the onPostExecute method. That way the UI Thread will start the task and then return to updating the Interface and acting on User Events.
精彩评论