1

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();
    }

 }
Al Lelopath
  • 6,448
  • 13
  • 82
  • 139
  • Is the task class an inner class of the Activity? – codeMagic Apr 30 '15 at 17:45
  • Yes, the `AsyncTask` is a class within `MainActivity`. I should have specified that. – Al Lelopath Apr 30 '15 at 17:46
  • Yeah, then use Petey's answer – codeMagic Apr 30 '15 at 17:46
  • Would it be possible if the `AsyncTask` class were not inside the `MainActivity` class? – Al Lelopath Apr 30 '15 at 17:52
  • 1
    Sure. By either getting a reference to the Dialog from the task or create a callback and use the activity variable. [Here is an example of the interface option](http://stackoverflow.com/questions/18517400/inner-class-can-access-but-not-update-values-asynctask/18517648?s=7|0.3001#18517648) – codeMagic Apr 30 '15 at 17:53

1 Answers1

1

change

private class LoadAndStoreDataTask extends AsyncTask   <String,  Integer, String> {

    private ProgressDialog nDialog;

to

private class LoadAndStoreDataTask extends AsyncTask   <String,  Integer, String> {

so the task will used the MainActivity nDialog field instead of the one in the LoadAndStoreDataTask class

petey
  • 16,914
  • 6
  • 65
  • 97
  • This does prevent the leak. As I said, all the examples I see have the `ProgressDialog` with the `AsycTask`. Does having it outside of the `AsyncTask` violate some best practices? – Al Lelopath Apr 30 '15 at 17:50
  • yea, its prevented by the asynctask not having the nDialog field and also your activity's `onPause` where you dismiss it. Looking at this again, It would be advisable to set nDialog to null right after the dismiss call and re-create it again whnever you run LoadAndStoreDataTask – petey Apr 30 '15 at 18:36