【问题标题】:onbackpressed make my app close without confirmationonbackpressed 让我的应用程序在没有确认的情况下关闭
【发布时间】:2016-04-25 02:47:23
【问题描述】:

我是新的 android 开发人员,当我从菜单中的任何页面按下返回时,我的应用程序将关闭。我用对话框添加了这段代码,但它不起作用

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

    FragmentManager fm = getSupportFragmentManager();
    int count = fm.getBackStackEntryCount();

    if(count == 0) {
        // Do you want to close app?
    }

}

【问题讨论】:

    标签: android pressed onbackpressed


    【解决方案1】:

    您是否尝试过将 super 调用放在 else 块中,以便仅在密钥不是 KEYCODE_BACK 时才调用它?

    /* Prevent app from being killed on back */
        @Override
        public boolean onKeyDown(int keyCode, KeyEvent event) {
    
            // Back?
            if (keyCode == KeyEvent.KEYCODE_BACK) {
                // Back
                moveTaskToBack(true);
                return true;
            }
            else {
                // Return
                return super.onKeyDown(keyCode, event);
            }
        }
    

    现在应该可以了!如果您有任何问题,请随时发表评论。

    【讨论】:

    • 我试过你的代码,它告诉我这个错误“KeyEvent不能被解析为一个类型”
    • @josepheshak 你需要导入 KeyEvent
    【解决方案2】:

    试试这个:

    @Override
    public void onBackPressed() {
        FragmentManager fm = getSupportFragmentManager();
        int count = fm.getBackStackEntryCount();
        if(count == 0) {
           // Do you want to close app?
           showDialog();
        }
    }
    
    public void showDialog() {
        AlertDialog.Builder alert = new AlertDialog.Builder(this);
        alert.setTitle("Do you want to close the app");
        alert.setPositiveButton("Confirm", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int whichButton) {
                  finish();  //or super.onBackPressed();
            }
        });
        alert.setNegativeButton("Dismiss", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int whichButton) {
            }
        });
        alert.show();
    }
    

    【讨论】:

      【解决方案3】:

      使用以下内容覆盖活动的 onBackPressed,以防您对某些值进行了更改但之后忘记更新这些值,而是按下了返回按钮:

      @Override
        public void onBackPressed() {
          if( <condition>) {
            AlertDialog.Builder ad = new AlertDialog.Builder( this);
            ad.setTitle("Changes were made. Do you want to exit without update?");
            ad.setPositiveButton(
                    "OK",
                    new DialogInterface.OnClickListener() {
                      @Override
                      public void onClick(DialogInterface dialog, int which) {
                        backPress();
                      }
                    }
            );
            ad.setNegativeButton(
                    "Update the changes",
                    new DialogInterface.OnClickListener() {
                      @Override
                      public void onClick(DialogInterface dialog, int which) {
                        <Update the changes>;
                        Toast.makeText(getApplicationContext(), "Changes were updated", Toast.LENGTH_SHORT).show();
                        backPress();
                      }
                    }
            );
            ad.setCancelable( false);
            ad.show();
          } else {
            backPress();
          }
        }
      
        private void backPress() {
          super.onBackPressed();
        }
      

      【讨论】:

        猜你喜欢
        • 2013-09-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-11-04
        • 2013-05-28
        • 2013-04-30
        • 2023-03-24
        • 1970-01-01
        相关资源
        最近更新 更多