David
David

Reputation: 23

AsyncTask onPostExecute not being called

The project I'm working on is slightly more complicated but I made this simple test to try to track down what was wrong with my code. The progress dialog never dismisses. I had it at one point where they weren't returning null. '

public class SyncTestActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        new mTask(this).execute();
    }

    public class mTask extends AsyncTask<Void, Void, Void> {

        Context mContext;

        ProgressDialog progressDialog;

        public mTask(Context aContext) {
            mContext = aContext;
        }

        @Override
        public void onPreExecute() {

            progressDialog = new ProgressDialog(mContext);
            progressDialog.setMessage("New...");
            progressDialog.show();
        }

        @Override
        public Void doInBackground(Void... params) {
            return null;
        }  

        public Void onPostExecute(Void... params) {
            progressDialog.dismiss();
            return null;


        }
    }

}

Upvotes: 2

Views: 6968

Answers (3)

Paresh Mayani
Paresh Mayani

Reputation: 128428

I agree with Cesar and Shailendra's answers, but let me make a little improvement over it:

    @Override
    protected void onPostExecute(Void result) {

      if(progressDialog.isShowing())
      {
        progressDialog.dismiss();
      }
        return;

    }

Upvotes: 5

Shailendra Singh Rajawat
Shailendra Singh Rajawat

Reputation: 8242

Missing @Override notation before onPostExecute. Also return null is not required.

Upvotes: 2

Cesar
Cesar

Reputation: 2057

The parameters are wrong, use this:

    @Override
    protected void onPostExecute(Void result) {
        progressDialog.dismiss();
        return;

    }

Upvotes: 14

Related Questions