【问题标题】:android onBackPressed method in fragment need help for exit from application片段中的android onBackPressed方法需要帮助才能退出应用程序
【发布时间】:2014-04-22 05:02:20
【问题描述】:

在我的一个应用程序中,我正在使用片段。现在我想在后退键按下时添加退出警报框,但我无法在 Fragment 扩展类中获取 public void onBackPressed() 方法。我怎样才能做到这一点?我的 MainActivity 扩展了 FragmentActivity。

public void onBackPressed() {   
    super.onBackPressed();
    alertbox("myappname", "Do You Want to Exit?");
}

但它不显示对话框,或者如果显示它会在 1 秒内消失。我做错了什么。

【问题讨论】:

标签: android fragment alert exit


【解决方案1】:

使用它退出

代码

 @Override
       public boolean onKeyDown(int keyCode, KeyEvent event) {
           if ((keyCode == KeyEvent.KEYCODE_BACK)) {
               AlertDialog.Builder alertbox = new AlertDialog.Builder(MainActivity.this);

               alertbox.setTitle("Do You Want To Exit ?");
               alertbox.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                   public void onClick(DialogInterface arg0, int arg1) { 
                      // finish used for destroyed activity
                       exit();
                   }
               });

               alertbox.setNegativeButton("No", new DialogInterface.OnClickListener() {
                   public void onClick(DialogInterface arg0, int arg1) {
                           // Nothing will be happened when clicked on no button 
                           // of Dialog     
                 }
               });

               alertbox.show();
           }
           return super.onKeyDown(keyCode, event);
       }
    public void exit()
    {
         Intent intent = new Intent(Intent.ACTION_MAIN);
         intent.addCategory(Intent.CATEGORY_HOME);
         intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
         startActivity(intent);
    }

【讨论】:

  • 试试这个代码...你会很容易退出后退键
  • onkeydown 不在片段中
【解决方案2】:

这个函数的工作原理和 backpressed 一样......警告框…………

@Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {

        // Handle the back button
        if (keyCode == KeyEvent.KEYCODE_BACK) {

            // Ask the user if they want to quit
            if (yourfragment.isVisible()) {
                new AlertDialog.Builder(this)

                        .setIcon(android.R.drawable.ic_dialog_alert)

                        .setTitle("Exit")

                        .setMessage("Are you sure you want to leave?")

                        .setNegativeButton(android.R.string.cancel, null)

                        .setPositiveButton(android.R.string.ok,
                                new DialogInterface.OnClickListener() {

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

                                        // Exit the activity

                                        finish();

                                    }

                                })

                        .show();

                // Say that we've consumed the event
                return true;

            }

        }

        return super.onKeyDown(keyCode, event);

    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-08-18
    • 1970-01-01
    • 2020-08-03
    • 1970-01-01
    • 2013-12-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多