Usman Riaz
Usman Riaz

Reputation: 3020

Activity leaked window token exception

I am showing user a dialog inside a runnable and giving him two buttons, YES and NO. If User presses NO, i am just finishing up the activity. if user presses YES i want to start another activity. Here is the dialog code:

ad.setTitle("Title");
                    ad.setMessage("Do you want to exit?");
                    ad.setIcon(R.drawable.common_signin_btn_icon_light);
                    ad.setButton(AlertDialog.BUTTON_POSITIVE, "Yes",
                            new DialogInterface.OnClickListener() {

                                @Override
                                public void onClick(DialogInterface dialog,
                                        int which) {

                                    // OPEN UP REGISTRATION ACTIVITY AGAIN
                                    ad.dismiss();
                            finish();
                            startActivity(new Intent(
                                    ActivityCodeVerification.this,
                                    ActivityRegister.class));
                                }
                            });
                    ad.setButton(AlertDialog.BUTTON_NEGATIVE, "No,Exit",
                            new DialogInterface.OnClickListener() {

                                @Override
                                public void onClick(DialogInterface dialog,
                                        int which) {
ad.dismiss();
                            finish();
                            startActivity(new Intent(
                                    ActivityCodeVerification.this,
                                    ActivityRegister.class));
                                }
                            });

Whenever any button gets pressed, I am getting this exception:

Activity com.Rp.chatout.ActivityCodeVerification has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@4055a530 that was originally added here

What am i doing wrong?? ad.show();

Upvotes: 0

Views: 48

Answers (2)

AADProgramming
AADProgramming

Reputation: 6345

You need to call dismiss() before exiting the Activity from where you are executing above code.

All windows & dialogs should be closed before leaving an Activity.

Upvotes: 1

artem
artem

Reputation: 16777

Add call method dismiss() in both cases before finishing Activity.

This exception raises because you are keeping reference to the Activity in your Dialog, while the Activity is already destroyed.

Upvotes: 2

Related Questions