zoukankan      html  css  js  c++  java
  • 关于android app两次点击返回键退出的处理

    现在的android app在开发时,引入了两次点击返回键退出app的设计

    为了避免用户误触,这个设计很人性化

    中文网上社区有些同学贴了一些实现的例子,我觉得不是很好

    代码如下

     1 public boolean onKeyDown(int keyCode, KeyEvent event) {
     2                 if (keyCode == KeyEvent.KEYCODE_BACK) {
     3                         if ((System.currentTimeMillis() - mExitTime) > 2000) {
     4                                 Object mHelperUtils;
     5                                 Toast.makeText(this, "再按一次退出程序", Toast.LENGTH_SHORT).show();
     6                                 mExitTime = System.currentTimeMillis();
     7 
     8                         } else {
     9                                 finish();
    10                         }
    11                         return true;
    12                 }
    13                 return super.onKeyDown(keyCode, event);
    14         }

    其中显示的调用了finish方法,更有甚者,显示的调用system.exit方法,以讹传讹,造成程序bug,降低程序的用户体验

    在android开发网的文档上可以看到:

    You can shut down an activity by calling its finish() method. 
    You can also shut down a separate activity that you previously started by calling finishActivity(). Note: In most cases, you should not explicitly finish an activity using these methods.
    As discussed in the following section about the activity lifecycle, the Android system manages the life of an activity for you,
    so you do not need to finish your own activities.
    Calling these methods could adversely affect the expected user experience and
    should only be used when you absolutely do not want the user to return to this instance of the activity.

    无论如何,主动去调用finish是懒人的烂代码,我翻遍文档找不到一篇文章会建议码农主动去调用finish的。

    在stackoverflow上,有个非常好的示例,代码如下,建议代码入坑的码农学习这个方案

     1 private static final int TIME_INTERVAL = 2000; // # milliseconds, desired time passed between two back presses.
     2 private long mBackPressed;
     3 
     4 @Override
     5 public void onBackPressed()
     6 {
     7     if (mBackPressed + TIME_INTERVAL > System.currentTimeMillis()) 
     8     { 
     9         super.onBackPressed(); 
    10         return;
    11     }
    12     else { Toast.makeText(getBaseContext(), "Tap back button in order to exit", Toast.LENGTH_SHORT).show(); }
    13 
    14     mBackPressed = System.currentTimeMillis();
    15 }
     1 private boolean doubleBackToExitPressedOnce;
     2 private Handler mHandler = new Handler();
     3 
     4 private final Runnable mRunnable = new Runnable() {
     5     @Override
     6     public void run() {
     7         doubleBackToExitPressedOnce = false;                       
     8     }
     9 };
    10 
    11 @Override 
    12 protected void onDestroy() 
    13 { 
    14     super.onDestroy();
    15 
    16     if (mHandler != null) { mHandler.removeCallbacks(mRunnable); }
    17 }
    18 
    19 @Override
    20 public void onBackPressed() {
    21     if (doubleBackToExitPressedOnce) {
    22         super.onBackPressed();
    23         return;
    24     }
    25 
    26     this.doubleBackToExitPressedOnce = true;
    27     Toast.makeText(this, "Please click BACK again to exit", Toast.LENGTH_SHORT).show();
    28 
    29     mHandler.postDelayed(mRunnable, 2000);
    30 }
  • 相关阅读:
    跟风!发一篇我常用VS开发技巧
    引用:程序员最常犯的五大非技术性错误
    Introduction to the Oracle Database 3
    Oracle Database 12c 12大新特性详解
    Streams全库复制
    Introduction to the Oracle Database 2
    Oracle FlashBack
    Oracle Database Features 2
    Oracle Database Features
    TNSName配置小结
  • 原文地址:https://www.cnblogs.com/jojodru/p/5443303.html
Copyright © 2011-2022 走看看