In the MainActivity
, I have an AsyncTask
in which a ProgressDialog
is displayed, starting in onPreExecute()
. If the processing in the doInBackground()
crashes, then I get this exception:
Activity us.nm.state.mmd.MainActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView
I believe it is occurring because the dialog is still being displayed while the activity is going down. The examples of AsyncTask/Progress dialog I see all have the dialog within theAsyncTask
class.
I would like to try dismissing the dialog in the Activity.onPause() method (as below), but since the dialog in declared in AsyncTask
class, MainActivity
does have access to it.
I moved the instantiation of the dialog to MainActivity
. So onPause()
does get the called and the call to dismiss()
happens, but this is long after the message "Unfortunately MyApp has stopped" appears on the device.
How do I do this?
public class MainActivity extends AppCompatActivity implements ActionBar.TabListener {
private ProgressDialog nDialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
nDialog = new ProgressDialog(mainActivity);
nDialog.setMessage(resources.getString(R.string.loading));
nDialog.setTitle(resources.getString(R.string.loading));
nDialog.setIndeterminate(false);
nDialog.setCancelable(false);
String url = "http://----------------------------------------";
Fragment myFragment = mTabsPageAdapter.getItem(TabsPagerAdapter.MY_TAB);
new LoadAndStoreDataTask((OnLoadAndStoreCompleteListener)permitsFragment, nDialog).execute(url);
}
@Override
public void onPause(){
super.onPause();
if(nDialog != null)
nDialog.dismiss();
}
}
private class LoadAndStoreDataTask extends AsyncTask <String, Integer, String> {
private ProgressDialog nDialog;
@Override
protected void onPreExecute() {
super.onPreExecute();
Resources resources = getResources();
nDialog.show();
}
protected String doInBackground(String... urls) {
....
}
@Override
protected void onPostExecute(String result) {
if (nDialog != null) {
nDialog.dismiss();
}
}
@Override
protected void onCancelled() {
if (nDialog != null) {
nDialog.dismiss();
}
}