I've been using dialog for the Asynctask
progress to show. By doing this, I initialized it inside onPreExecute
and dismissed it in onPostExecute
. I'm not sure what went wrong when I placed a condition to dismiss only if it's not null and it's showing but still triggered IllegalArgumentException: View not attached to window manager
Here's the code:
public class SampleTask extends AsyncTask<String, Void, String> { public ProgressDialog pDialog; @Override protected void onPreExecute() { super.onPreExecute(); pDialog = new ProgressDialog(SampleActivity.this); pDialog.setMessage(getString(R.string.loading)); pDialog.setCancelable(false); pDialog.show(); } @Override protected String doInBackground(String... path) { . . . } @Override protected void onPostExecute(String result) { super.onPostExecute(null); if (null != pDialog && pDialog.isShowing()) { pDialog.dismiss(); } . . . } }
I read here similar solution but would it differ if I place null != pDialog && pDialog.isShowing()
together in the same condition?