简体   繁体   中英

how to dismiss alertdialog when it's Yes button is Clicked and not show it again onBackPressed?

i have two alert dialog s in my application.

1 is for exit application and another is for redirect to url.

i have shown dialog inside dialog.

first dialog will be pop up is to redirect url now in that when user press it will redirect to url.

But when user press back button then i have to dismiss it and want to show only exit dialog on back button click.

here is my code.

boolean flag = false;

@Override
public void onBackPressed() {
    if (flag) {
        super.onBackPressed();

    } else {
        final AlertDialog.Builder alert = new AlertDialog.Builder(
                MainActivity.this);
        alert.setMessage("EXIT"); // Message
                                                                                // here

        alert.setPositiveButton("NO",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog,
                            int whichButton) {
                        AlertDialog.Builder exitdialog = new AlertDialog.Builder(
                                MainActivity.this);
                        exitdialog
                                .setMessage("EXIT");
                        exitdialog.setTitle(R.string.app_name);
                        exitdialog.setIcon(R.drawable.logo);
                        exitdialog.setPositiveButton("NO",
                                new OnClickListener() {

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

                                    }
                                });
                        exitdialog.setNegativeButton("Yes",
                                new OnClickListener() {

                                    @Override
                                    public void onClick(
                                            DialogInterface dialog,
                                            int which) {
                                        /*
                                         * Intent i=new
                                         * Intent(getApplicationContext
                                         * (),MainActivity.class);
                                         * startActivity(i);
                                         */

                                        finishFromChild(getParent());
                                        getIntent()
                                                .setFlags(
                                                        Intent.FLAG_ACTIVITY_CLEAR_TOP);
                                        finish();
                                    }
                                });
                        AlertDialog alertDialog = exitdialog.create();
                        alertDialog.show();

                    }
                });

        alert.setNegativeButton("YES",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog,
                            int whichButton) {

                        Intent viewTwitter = new Intent(
                                "android.intent.action.VIEW",
                                Uri.parse("URL"));
                        startActivity(viewTwitter);
                        overridePendingTransition(R.anim.fadein,
                                R.anim.fadeout);

                    }

                });
        final AlertDialog alertDialog = alert.create();
        alertDialog.show();

    }

Remove finish(); default action is dismiss Put dialog.dismiss(); in side onClick method

Edit set flag = true;

boolean flag = false;

@Override
public void onBackPressed() {
if (flag) {
    super.onBackPressed();

} else {
    final AlertDialog.Builder alert = new AlertDialog.Builder(
            MainActivity.this);
    alert.setMessage("EXIT"); // Message
                                                                            // here

    alert.setPositiveButton("NO",
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog,
                        int whichButton) {
                    AlertDialog.Builder exitdialog = new AlertDialog.Builder(
                            MainActivity.this);
                    exitdialog
                            .setMessage("EXIT");
                    exitdialog.setTitle(R.string.app_name);
                    exitdialog.setIcon(R.drawable.logo);
                    exitdialog.setPositiveButton("NO",
                            new OnClickListener() {

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

                                }
                            });
                    exitdialog.setNegativeButton("Yes",
                            new OnClickListener() {

                                @Override
                                public void onClick(
                                        DialogInterface dialog,
                                        int which) {
                                    /*
                                     * Intent i=new
                                     * Intent(getApplicationContext
                                     * (),MainActivity.class);
                                     * startActivity(i);
                                     */

                                    finishFromChild(getParent());
                                    getIntent()
                                            .setFlags(
                                                    Intent.FLAG_ACTIVITY_CLEAR_TOP);
                                    finish();
                                }
                            });
                    AlertDialog alertDialog = exitdialog.create();
                    alertDialog.show();

                }
            });

    alert.setNegativeButton("YES",
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog,
                        int whichButton) {
             flag= true;

                    Intent viewTwitter = new Intent(
                            "android.intent.action.VIEW",
                            Uri.parse("URL"));
                    startActivity(viewTwitter);
                    overridePendingTransition(R.anim.fadein,
                            R.anim.fadeout);

                }

            });
    final AlertDialog alertDialog = alert.create();
    alertDialog.show();

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM