简体   繁体   中英

How do I dismiss a progress dialog with async task and avoid “is your activity running error?”

I'm having trouble avoiding a "Unable to add window — token android.os.BinderProxy is not valid; is your activity running?" exception when using a progress dialog box with an async task.

final ProgressDialog nDialog = new ProgressDialog(MainActivity.this);
    nDialog.setMessage("Loading...");
    nDialog.setIndeterminate(false);
    nDialog.setCancelable(false);
    if(!isFinishing()){nDialog.show();}

I then continue with:

final Handler handler = new Handler();
    handler.postDelayed(new Runnable() {
        @Override
        public void run() {
           //Run code while showing progress dialog
           ndialog.dismiss
}
    }, 1000);

I've come to understand that I'll occasionally get an exception because the activity will have finished, while the ndialog is being accessed. So a solution seems to be change the ndialog.

The initial issue of showing a dialog once the activity finishes (which only happens rarely) is solved by

if(!isFinishing()){nDialog.show();}

I was considering putting this same code for the nDialog.dismiss. But the problem is that if I do:

if(!isFinishing()){nDialog.dismiss();}

and the activity finishes before this can run, the user will get stuck with a dialog screen that will never get dismissed.

Am I missing something? How can I prevent this error, but at the same time make sure that the dialog will start and be dismissed?

Thanks!

Careful while handling views, activities and other related UI objects from another thread. Threads like AsyncTask are not aware of the activity lifecycle, and you may end up posting things to dead windows. I believe this is what is happening to you.

A safer way to do this:

import android.os.AsyncTask;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;

import java.lang.ref.WeakReference;

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

private final WeakReference<MyActivity> weakReferenceActivity;

public MyTask(@NonNull MyActivity activity) {
    this.weakReferenceActivity = new WeakReference<>(activity);
}

@Nullable
public MyActivity getActivity() {
    MyActivity activity = weakReferenceActivity.get();
    if (activity.isDestroyed()) {
        return null;
    }
    return activity;
}

@Override
protected void onPreExecute() {
    MyActivity activity = getActivity();
    if (activity != null) {
        activity.showProgressDialog();
    }
}

@Override
protected Void doInBackground(Void... voids) {
    [do something]
    return null;
}

@Override
protected void onPostExecute(Void nothing) {
    MyActivity activity = getActivity();
    if (activity != null) {
        activity.hide();
    }
}
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM