0

While I'm updating my database I want to display a progress dialog. My problem is that the ProgressDialog is getting late to appear,after 4-5 seconds, then appears and disappears very fast, it stays on screen few milliseconds almost you can't see it, then new data are shown in the list immediately. This makes me think that the ProgressDialog is waiting for database to be updated(it doesn't take much, about 4,5 seconds) and then it shows on the screen but is dismissing very fast. I would like the ProgressDialog appear immediately I press the 'Update' button and stay on the screen about 4-5 seconds.

class MyAsyncTask extends AsyncTask<Void, Void, Void>{

        ProgressDialog myprogsdial;
        @Override
        protected void onPreExecute(){
            myprogsdial = ProgressDialog.show(MyActivity.this, null, "Upgrade", true);
        }

        @Override
        protected Void doInBackground(Void... params) {
            // TODO Auto-generated method stub

                runOnUiThread(new Runnable() {

                    @Override
                    public void run() {
                        // TODO Auto-generated method stub

                        RefreshDataBase();

                    }
                });

            return null;
        }

        @Override
        protected void onPostExecute(Void result){
            myprogsdial.dismiss();
        }

    }

When I call it, new MyAsyncTask().execute();

inazaruk
  • 74,247
  • 24
  • 188
  • 156
AlexAndro
  • 1,918
  • 2
  • 27
  • 53
  • seems like you have no idea what you're doing ... why you're using `runOnUiThread` in `doInBackground`? ... now your code flow is like: i'm starting new thread(AsyncTask) to run job back on ui thread(runOnUiThread) – Selvin Jan 11 '12 at 17:06
  • what happens when you execute ProgressDialog.show(...) call before you invoke new MyAsyncTask().execute(), and maintain a final reference (to your progress dialog) inside of originating code? – bgs Jan 11 '12 at 17:10

3 Answers3

6

Ok I think that this

runOnUiThread(new Runnable() {

is causing this behavior.

doInBackground() executes your code in a new thread to the main UI thread. You are then putting the code to execute in this thread back into the main one causing the progress dialog to be delayed at the end and then in postExecute() it gets closed immediately.

A good asyntask tutorial can be found here.

Graham Smith
  • 25,627
  • 10
  • 46
  • 69
2

You must not use runOnUiThread. What you're basically did is:

  1. Started new non-ui thread
  2. From this new non-ui thread you posted a long running task to UI thread.
  3. Exited from non-ui thread.
  4. Your ui thread now executes long-running operation (RefreshDataBase) and blocks the UI.

You should call RefreshDataBase() directly. And if this method touches UI, you have to refactor it.

inazaruk
  • 74,247
  • 24
  • 188
  • 156
  • I have tried to call `RefreshDataBase` directly, but it crash with: `Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()`. What do you mean, I have to refactor it? – AlexAndro Jan 11 '12 at 17:22
  • That's because it uses something that should run on UI thread (or at least on Looper thread). – inazaruk Jan 11 '12 at 18:50
2

I have solved it, using this answer of Vladimir Ivanov. I have separated the functionality by the appearance. I have kept the functionality(downloading new data) in doInBackground() and in onPostExecute() I updated the list: get the new adapter,called setListAdaper() and notifyDataSetChanged. Of course, I quit using runOnUiThread(). Thanks to all for hints.

Community
  • 1
  • 1
AlexAndro
  • 1,918
  • 2
  • 27
  • 53