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 }
  • 相关阅读:
    在Android中如何获取视频的第一帧图片并显示在一个ImageView中
    利用MsChart控件绘制多曲线图表 z
    国外成熟的程序交易系统的思路 z
    稳健获利
    用vmware安装gho文件
    数学之美 zt
    大型邮箱smtp服务器及端口 收集
    英语之路 zt
    C# Get Desktop Screenshot ZZ
    C#/PHP Compatible Encryption (AES256) ZZ
  • 原文地址:https://www.cnblogs.com/jojodru/p/5443303.html
Copyright © 2011-2022 走看看