ProgressDialog during startActivity()

791 Views Asked by At

I have this code in my activity (LoadTask is an inner class):

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    ...
    ProgressDialog loading = new ProgressDiaolg(this);
    ...
    }


public class LoadTask extends AsyncTask<Context, Void, Integer> {
    ArrayList<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();

    @Override
    protected void onPreExecute() {
        loading.show();
    }

    @Override
    protected Integer doInBackground(Context... params) {
        EventLoader el = new EventLoader(params[0]);
        list = el.getMylist();
        return el.getError();
    }
    protected void onPostExecute(Integer result) {

        if (result == EventLoader.NO_HTTP_CONN) {
            getErrorDialog(R.string.errore_caricamento, true).show();
        } else if (result == EventLoader.ERR_JSON) {
            getErrorDialog(R.string.errore_json, true).show();
        } else if (result == EventLoader.ERR_NULL) {
            getErrorDialog(R.string.errore_evento_null, true).show();
        } else {
            mainList=list;
            Intent intent = new Intent(MainActivity.this, EventActivity.class);
            intent.putExtra(Const.Tags.FULL_LIST, mainList);

            loading.dismiss();
            startActivity(intent);
        }
    }
}

the dialog works fine during the loading of the list, but here's my problem:

  1. dismissing the dialog before startActivity(intent) will result in 2-3 seconds of no progress indicator while the new activity is created (there's a listview to be populated with a custom adapter) and before it is actually resumed, which is not very user friendly.
  2. dismissing the dialog after startActivity(intent) will result in a rough disposal of the dialog (first goes the dimmed background, then it freezes... and so on). actually it's like not calling loading.dismiss() at all and letting it be destroyed with the activity.

Is there a smooth way for the ProgressDialog to be persistent through both the loading of the data and the creation of the new activity and eventually be dismissed when the new activity UI is ready to be shown?

1

There are 1 best solutions below

0
On

If you want a smoother transition I would recommend you use fragments instead. Or you can make it slightly smoother by not populating the list view until onStart and making sure it's done on the background thread.