Search code examples
androidandroid-asynctaskprogressdialog

ProgressDialog appears too late and dissapears too fast


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();


Solution

  • 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.