按一次返回鍵,出現「再按一次返回鍵,退出本APP」提示,
再按一次返回鍵,才跳出APP返回桌面。
[方法]
覆寫 onBackPressed() 方法
...... //連按兩下返回鍵才退出APP private boolean doubleBackToExitPressedOnce = false; @Override public void onBackPressed() { if (doubleBackToExitPressedOnce) { super.onBackPressed(); return; } this.doubleBackToExitPressedOnce = true; Snackbar.make(this.getWindow().getDecorView().findViewById(android.R.id.content), "再按一次返回鍵,退出本APP", Snackbar.LENGTH_SHORT).show(); new Handler().postDelayed(new Runnable() { @Override public void run() { doubleBackToExitPressedOnce = false; } }, 2000); } ......
參考:
- https://stackoverflow.com/questions/8430805/clicking-the-back-button-twice-to-exit-an-activity?page=2&tab=votes
java - Clicking the back button twice to exit an activity - Stack Overflow - https://www.jianshu.com/p/78de93f71965
最常用的Activity的onBackPressed()与finish()的区别. - 简书
沒有留言:
張貼留言