Android, the Standby Dialog Box Does Not Appear at All

advertisements

I want to show the waiting dialog but it doesn't show at all, which is weird because the thing that takes a lot of time is the setData() function where i get a json feed and I save them in database (it takes 3/5 seconds).

Here is my code :

    ProgressDialog dialog = ProgressDialog.show(TabLayoutActivity.this, "", "Loading. Please wait...", true);

    //refresh the view
    taskLiteApplication.setData();

    //change view
    Intent myIntent = new Intent(TabLayoutActivity.this, TabLayoutActivity.class);
    TabLayoutActivity.this.startActivity(myIntent);

    //kill the dialog waiting
    dialog.cancel();


I bet the issue is the fact that you are doing the all of this work on the UI thread, so the moment you start the dialog, the thread that starts and shows the window is actually freezing while you do the setData() work. You really should move the setData function to a background thread or a AsyncTask so that the UI thread can continue while you do the heavy work in the background.

private ProgressDialog dialog;

void doStuff()
{
    dialog = ProgressDialog.show(TabLayoutActivity.this, "", "Loading. Please wait...", true);
    HeavyTask task = new HeavyTask();
    task.execute();
}

private class HeavyTask extends AsyncTask<Void, Void, Void> {
     protected Void doInBackground(Void... args) {
         //refresh the view
         taskLiteApplication.setData();
         return;
     }

     protected void onPostExecute(Void results) {
         //change view
         Intent myIntent = new Intent(TabLayoutActivity.this, TabLayoutActivity.class);
         TabLayoutActivity.this.startActivity(myIntent);

         //kill the dialog waiting
         dialog.dismiss();
         dialog = null;
     }
 }