user1318194
user1318194

Reputation:

Showing an AlertDialog once the activity has closed

I am in the habit of passing this to any methods that take a Context. If I am inside an inner class, I pass OuterClassName.this. This may be bad practice, but I've never found a clear answer to what I should be doing.

I have an activity that loads some JSON data using Volley, and if there is an error the onError callback displays a Dialog (constructed with OuterClassName.this as the Context).

However, if the user has left that activity (pressed the back button) by the time an error occurs, the app crashes:

android.view.WindowManager$BadTokenException: Unable to add window -- token android.os.BinderProxy@422a88e8 is not valid; is your activity running?

Of course, my activity isn't running. But I still want to display the Dialog. How can I achieve this?

Upvotes: 2

Views: 1298

Answers (2)

waqaslam
waqaslam

Reputation: 68177

By design, you should not to show any Dialog once your activity is finished. So, in your case, to stop popping the dialog window and avoiding BadTokenException, you may try something like this:

if(!YourActivity.this.isFinishing()){
   //show dialog on error
}

However, if you still insist on popping the dialog then what you may do is:

if(!YourActivity.this.isFinishing()){
   //show dialog on error
}
else{
   //launch a new activity which should take care of error msg dialog
}

Upvotes: 4

KOTIOS
KOTIOS

Reputation: 11194

  1. Passing OuterClass.this is valid from inner class
  2. When Activity is killed the view realted to the same wont be alive .

If you still wanna show some dialog after the crash end i would say create an activity as dialog and start that activity from application context . so even if you main activity is dead this activity will start after some background process . You can use parent="android:Theme.Dialog" as theme for creating activity as dialog

Upvotes: 0

Related Questions