0

I wanted a progress dialog to appear while my activity gets some data. I used Async task but however it doesn't show up I have tried all the answers of the previous Stack Overflow questions but none of them seem to work for me

private class BackgroundSync extends AsyncTask<Void,Void,Void> {
    ProgressDialog progress = new ProgressDialog(MainActivity.this);

    @Override
    protected void onPreExecute() {
        progress.setMessage("Loading");
        progress.setTitle("Loading");
        progress.show();
        super.onPreExecute();



    }

    @Override
    protected Void doInBackground(Void... voids) {
        /get data

        return null;
    }

    @Override
    protected void onPostExecute(Void aVoid) {

        progress.dismiss();
        super.onPostExecute();

    }
}

I have also tried this using progress = ProgressDialog.show(MainActivity.this,"Loading","Loading"); as one stack overflow answer suggested but it still doesn't show

I also used the below code, but this time the ProgressDialog doesn't disappear

private class BackgroundSync extends AsyncTask<Void,Void,Void> {
    ProgressDialog progress

    @Override
    protected void onPreExecute() {
        progress = new ProgressDialog(MainActivity.this);
        progress.show(MainActivity.this,"Loading","Loading");
        super.onPreExecute();

    }

    @Override
    protected Void doInBackground(Void... voids) {
        SyncEvents();

        return null;
    }

    @Override
    protected void onPostExecute(Void aVoid) {

        progress.dismiss();

    }
}`

What do I do? This ProgressDialog issue has been taking more than 2 hours to solve

Markus Kauppinen
  • 3,025
  • 4
  • 20
  • 30
Aaron
  • 1
  • 2

1 Answers1

0

use this code

private class BackgroundSync extends AsyncTask<Void, Void, Void> {
    private ProgressDialog dialog;

    public BackgroundSync(MyMainActivity activity) {
        dialog = new ProgressDialog(activity);
    }

    @Override
    protected void onPreExecute() {
        dialog.setMessage("Loading");
        dialog.setTitle("Loading");
        dialog.show();
    }
    @Override
    protected Void doInBackground(Void... args) {
        // do background work here
        return null;
    }
    @Override
    protected void onPostExecute(Void result) {
         // do UI work here
        if (dialog.isShowing()) {
            dialog.dismiss();
        }
    }
}
shareque
  • 13
  • 8