關於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 }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM