android - should ProgressDialog be checked for null before dismissed?

2.2k Views Asked by At

If I use ProgressDialog.show() on main which does call 2 AsyncTask. AsyncTask A and AsyncTask B may finish in undetermined order so I put ProgressDialog.dismiss() in onPostExecute in both AsyncTask.

The question is should I check for null value in both AsyncTask before dismissing the dialog?

if(progressDialog != null && progressDialog.isShowing()){ progressDialog.dismiss(); }

OR just:

progressDialog.dismiss();

I don't know if ProgressDialog.dismiss() will handle this for me or it will throw NullPointerException if I don't check for null value before dismissing.

2

There are 2 best solutions below

0
On

java says this is always good to check for null . but you mustprogressDialog.isShowing() otherwise you can get a leak window exception sometime.

0
On

You do not need to check isShowing() because it is internally handled. No need to handle it explicitly.