【问题标题】:My Application is not exiting when I double tap the Hardware Back Button当我双击硬件返回按钮时,我的应用程序没有退出
【发布时间】:2015-05-26 09:27:58
【问题描述】:

当用户双击硬件后退按钮时,我试图退出我的应用程序,我在我的应用程序中使用了以下代码:

    if (doubleBackToExitPressedOnce) {
        super.onBackPressed();
        Dashboard_Activity.this.finish();
        return;
    }

    this.doubleBackToExitPressedOnce = true;
    Toast.makeText(this, "Please click BACK again to exit",
            Toast.LENGTH_SHORT).show();

    new Handler().postDelayed(new Runnable() {

        @Override
        public void run() {
            doubleBackToExitPressedOnce = false;
        }
    }, 2000);

在这里,当用户双击硬件返回按钮时,相同的活动会一次又一次地出现,但应用程序不会退出。你能帮我解决这个问题吗?

【问题讨论】:

  • 您好,您是否尝试将此添加到您的活动中? Dashboard_Activity.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
  • 2 个问题:首先你为什么放这个 super.onBackPressed(); , 验证活动实例,活动是否有多个实例
  • 在这个 Dashboard_Activity 之前还有其他活动吗?
  • 你是在 onFinish() 还是 onDestroy() 做点什么?
  • 你只需要单次反压使用 onBackpressed{ finish();}

标签: android exit back-button onbackpressed


【解决方案1】:

尝试以下方法:

private static long back_pressed;

    @Override
    public void onBackPressed()
    {
        if (back_pressed + 2000 > System.currentTimeMillis()) super.onBackPressed();
        else Toast.makeText(getBaseContext(), "Press once again to exit!", Toast.LENGTH_SHORT).show();
        back_pressed = System.currentTimeMillis();
    }

Source

【讨论】:

    【解决方案2】:

    在代码中的任何位置尝试此方法在 Oncreate 之外,为我工作

            @Override
            public void onBackPressed() 
            {
                try
                {
    
                //pop up Window closed. 
                if (doubleBackToExitPressedOnce)
                {
                    super.onBackPressed();
                    return;
                }
    
                this.doubleBackToExitPressedOnce = true;
                Toast.makeText(this, "Please click BACK again to exit", Toast.LENGTH_SHORT).show();
    
                new Handler().postDelayed(new Runnable()
                {
    
                    @Override
                    public void run() 
                    {
                        doubleBackToExitPressedOnce=false;                       
                    }
                }, 2000);
    
                }
                catch(Exception ex)
                {
                    ex.printStackTrace();
                }
            }   
    

    【讨论】:

      【解决方案3】:

      首先,创建SplashScreenActivity

      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          finish();
      }
      

      然后在您的活动中(或在 BaseActivity 中)将此变量添加为类成员:

      private long milis;
      

      并覆盖onBackPressed():

      @Override
      public void onBackPressed() {
          if(milis + 2000 > System.currentTimeMillis()){
              Intent intent = new Intent(this, SplashScreenActivity.class);
              intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
              startActivity(intent);
          }else{
              milis = System.currentTimeMillis();
          }
      }
      

      【讨论】:

        猜你喜欢
        • 2020-10-12
        • 2012-04-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多