Troglodyte
Troglodyte

Reputation: 13

Android - AlertDialog Causes android.view.WindowsLeaked

I am trying to run another app from my own. If the other app isn't installed, then I want a dialog to pop which ask wheater you want to install it or not and if you do, then it will open market for you. However, I get this error. What is this error? What causes it and how can I solve it?

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    barcodeAlive = true;

    try {

        startActivityForResult(new Intent(
                "com.google.zxing.client.android.SCAN").putExtra(
                "SCAN_MODE", "QR_CODE_MODE"), 0);
    } catch (ActivityNotFoundException anfe) {

        new Handler().postDelayed(new Runnable() {

            @Override
            public void run() {

                showDownloadDialog(BarcodeActivity.this,
                        "QR Reader could not found..",
                        "Do you want to download QR Reader ? ", "Yes", "No");
            }
        }, DELAY);
    }
}

private static AlertDialog showDownloadDialog(final Activity activity,
        CharSequence stringTitle, CharSequence stringMessage,
        CharSequence stringButtonYes, CharSequence stringButtonNo) {

    final AlertDialog.Builder downloadDialog = new AlertDialog.Builder(
            activity);
    downloadDialog.setTitle(stringTitle);
    downloadDialog.setMessage(stringMessage);
    downloadDialog.setPositiveButton(stringButtonYes,
            new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialogInterface, int i) {

                    Uri uri = Uri.parse("market://search?q=pname:"
                            + PACKAGE);
                    try {
                        activity.startActivity(new Intent(
                                Intent.ACTION_VIEW, uri));
                    } catch (ActivityNotFoundException anfe) {

                    } catch (Exception e) {
                    }
                }
            });
    downloadDialog.setNegativeButton(stringButtonNo,
            new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialogInterface, int i) {

                    downloadDialog
                            .getContext()
                            .startActivity(
                                    new Intent(downloadDialog.getContext(),
                                            MainActivity.class)
                                            .setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY));
                }
            });
    downloadDialog.setCancelable(false);
    return downloadDialog.show();
}

Upvotes: 1

Views: 71

Answers (1)

Nova Entropy
Nova Entropy

Reputation: 5767

You should dismiss() your dialog before starting the other Activity. If you leave it open, you will leak the window.

Upvotes: 1

Related Questions