【问题标题】:How to prevent user from exiting an app when back button is pressed [duplicate]按下后退按钮时如何防止用户退出应用程序[重复]
【发布时间】:2014-03-31 12:03:47
【问题描述】:

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

【问题讨论】:

    标签: android android-activity back


    【解决方案1】:

    通过覆盖Activity 类的onBackPressed() 方法来实现类似的效果:

    @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();
    
    
    }
    

    【讨论】:

      【解决方案2】:

      做吧,

         @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();
      
      
      
      }
      

      【讨论】:

        【解决方案3】:

        在你的活动中覆盖 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();
        
        
        
            }
        

        【讨论】:

          【解决方案4】:

          试试这个方法

           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();
          
              }
          
          }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2019-06-17
            • 2014-01-02
            • 1970-01-01
            • 1970-01-01
            • 2012-05-10
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多