Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error : BinderProxy@45d459c0 is not valid; is your activity running?

Tags:

android

This is most likely happening because you are trying to show a dialog after execution of a background thread, while the Activity is being destroyed.

I was seeing this error reported once in a while from some of my apps when the activity calling the dialog was finishing for some reason or another when it tried to show a dialog. Here's what solved it for me:

if(!((Activity) context).isFinishing())
{
    //show dialog
}

I've been using this to work around the issue on older versions of Android for several years now, and haven't seen the crash since.

2021 Update

It's been noted in some of the comments that it's bad to blindly cast Context to an Activity. I agree!

When I'm writing similar code these days in a Fragment (8+ years after the original answer was provided), I do it more like this:

if (!requireActivity().isFinishing) {
     // show dialog
}

The main takeaway is that trying to show a dialog or update any UI after the hosting Activity has been killed will result in a crash. Do what you can to prevent that by killing your background threads when your Activity is killed, or at a minimum, use the answer here to stop your app from crashing.


I faced the same problem and used the code proposed by DiscDev above with minor changes as follows:

if (!MainActivity.this.isFinishing()){
    alertDialog.show();
}

if dialog is trowing this problem because of the thread you should do run this on UI thread like that :-

runOnUiThread(new Runnable() {
            @Override
            public void run() {
                dialog.show();

            }
        });

This error occurs when you are showing the dialog for a context that no longer exists.

Before calling .show() check that activity/context is not finishing

if (!(context instanceof Activity && ((Activity) context).isFinishing())) {
    alert.show();
}

I encountered this error when I had a countDownTimer in my app. It had a method calling GameOver in my app as

public void onFinish() {
     GameOver();
}

but actually the game could be over before the time was up due to a wrong click of the user (it was a clicking game). So when I was looking at the Game Over dialog after e.g. 20 seconds, I forgot to cancel the countDownTimer so once the time was up, the dialog appeared again. Or crashed with the above error for some reason.


The fix to this is pretty simple. Just test if the Activity is going through its finishing phase before displaying the Dialog:

  private Handler myHandler = new Handler() {
  @Override
  public void handleMessage(Message msg) {
    switch (msg.what) {
      case DISPLAY_DLG:
        if (!isFinishing()) {
        showDialog(MY_DIALOG);
        }
      break;
    }
  }
};

see more here


In my case, the problem was that Context was kept as a weak reference in the class that extends Handler. Then I was passing Messenger, that wraps the handler, through an Intent to a Service. I was doing this each time the activity appeared on screen in onResume() method.

So as you understand, Messenger was serialized together with its fields (including context), because it is the only way to pass objects using Intent - to serialize them. At that moment when Messenger was passed to the service, the activity itself still wasn't ready for showing dialogs as it is in another state (being said onResume(), that is absolutely different from when the activity is already on the screen). So when messenger was deserialized, the context still was at the resuming state, while the activity actually was already on the screen. Moreover, deserialization allocates memory for new object, that is completely different from the original one.

The solution is just to bind to the service each time you need it and return a binder that has a method like 'setMessenger(Messenger messenger)' and call it, when you are binded to the service.