如何在按下后退按钮时阻止用户退出应用程序

时间:2014-03-31 12:03:48

标签: android android-activity back

我开发了一个成功运行的应用程序,但有两个活动,第一个是启动画面,第二个是webview。现在的问题是当按下后退按钮时它会关闭应用程序并退出,但是应用程序中的后退,前进和刷新按钮用于内部导航。因此,如何显示是否确定退出对话框或阻止用户停用后退键。

4 个答案:

答案 0 :(得分:10)

通过覆盖onBackPressed()类的Activity方法:

来完成此类操作
@Override
public void onBackPressed() {
    new AlertDialog.Builder(this)
            .setMessage("Are you sure you want to exit?")
            .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    MyActivity.super.onBackPressed();
                }
            })
            .setNegativeButton("No", null)
            .show();


}

答案 1 :(得分:1)

做,

   @Override
public void onBackPressed() {

  //  super.onBackPressed();

            AlertDialog.Builder alertDialog = new AlertDialog.Builder(this);

            // Setting Dialog Title
            alertDialog.setTitle("Exit alert");
             alertDialog.setMessage("Do You want Exit??");
            alertDialog .setIcon(R.drawable.galleryalart);


            alertDialog.setNegativeButton("NO", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog,int which) {

                          dialog.cancel();
                }
            });


            alertDialog.setPositiveButton("YES", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {

             [youractivity].super.onBackPressed();


                }
            });


            alertDialog.show();



}

答案 2 :(得分:0)

在您的活动中覆盖onBackPressed

 @Override
    public void onBackPressed() {

        super.onBackPressed();

                AlertDialog.Builder alertDialog = new AlertDialog.Builder(SettingsActivity.this);

                // Setting Dialog Title
                alertDialog.setTitle("Signout your app");


                alertDialog.setNegativeButton("NO", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog,int which) {

                              dialog.cancel();
                    }
                });


                alertDialog.setPositiveButton("YES", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {



                    }
                });


                alertDialog.show();



    }

答案 3 :(得分:0)

试试这种方式

 public class YourActivity extends Activity {

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

    }

    @Override
    public void onBackPressed() {

        AlertDialog.Builder builder = new AlertDialog.Builder(this);

        int imageResource = android.R.drawable.ic_dialog_alert;
        Drawable image = getResources().getDrawable(imageResource);

        builder.setTitle("Exit").setMessage("want to exit?").setIcon(image).setCancelable(false).setPositiveButton("yes", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {
                finish();
            }
        }).setNegativeButton("no", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int id) {
            }
        });

        AlertDialog alert = builder.create();
        alert.setCancelable(false);
        alert.show();

    }

}