Issue
I have a progress dialog that I need to dismiss and then finish the app. The way I'm doing it is like this:
new AlertDialog.Builder(MainActivity.this)
.setMessage("There has been a problem.")
.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
public void onClick(
DialogInterface dialog,
int which) {
dialog.dismiss();
finish();
}
}).show();
Nevertheless, I always get the famous error:
Caused by: android.view.WindowManager$BadTokenException: Unable to add window -- token android.app.LocalActivityManager$LocalActivityRecord@48189240 is not valid; is your activity running?
05-23 16:56:44.573: ERROR/AndroidRuntime(2494): at android.view.ViewRoot.setView(ViewRoot.java:505)
05-23 16:56:44.573: ERROR/AndroidRuntime(2494): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:200)
05-23 16:56:44.573: ERROR/AndroidRuntime(2494): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:114)
05-23 16:56:44.573: ERROR/AndroidRuntime(2494): at android.view.Window$LocalWindowManager.addView(Window.java:424)
05-23 16:56:44.573: ERROR/AndroidRuntime(2494): at android.app.Dialog.show(Dialog.java:241)
05-23 16:56:44.573: ERROR/AndroidRuntime(2494): at android.app.ProgressDialog.show(ProgressDialog.java:107)
05-23 16:56:44.573: ERROR/AndroidRuntime(2494): at android.app.ProgressDialog.show(ProgressDialog.java:90)
05-23 16:56:44.573: ERROR/AndroidRuntime(2494): at android.app.ProgressDialog.show(ProgressDialog.java:85)
How could I dismiss it without problems? am I doing it right?
Thanks a lot in advance.
EDIT: I can get to show it perfectly, and even when dismissing it, the user doesn't see anything wrong, but in my LogCat I can see that nasty error.
Solution
I haven't tested this, though it should work. However, as an alternate, you may call finish() in your dialog's onClick and in your activity's onStop call dialog.dismiss().
Calling dismiss in onStop will avoid the window leak warning message.
Answered By - waqaslam
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.