我不希望我的活力在按下「後退」按鈕時被破壞。我的應用程序與1.6 SDK兼容。參照http://android-developers.blogspot.com/2009/12/back-and-other-hard-keys-three-stories.html和Override back button to act like home button,我選擇以下代碼:不要調用onDestroy on onBackPressed
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
// For versions lower than 2.0
if (Utility.buildDet.getDeviceBuildAPI() <= Utility.buildDet.getBuildApi()
&& keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0)
onBackPressed();
return super.onKeyDown(keyCode, event);
}
// In any version, this function will be called
public void onBackPressed() {
// This will be called either automatically for you on 2.0
// or later, or by the code above on earlier versions of the platform.
Log.i(TAG, "##### BACK KEY IS PRESSED");
this.moveTaskToBack(true); // on false, it shows moveTaskToBack: 11
return;
}
當我按下返回按鈕,我這些日誌
: ##### BACK KEY IS PRESSED
INFO/ActivityManager(51): moveTaskToBack: 10
: !!!!!!! Into onPause
: !!!!!!! Into onStop
: !!!!!!! Into DESTROY
我還沒有重寫moveTasToBack()。 Anu clu當按下後退按鈕時,我該怎麼做纔不會被破壞。也許我想忽略按鈕或隱藏活動。
任何線索,爲什麼它不按預期工作。
感謝
感謝Adil,它與1.6和2.1+一起工作。我希望它適用於需要onBackPressed的Droid 2.0+的真實設備(如在搜索查詢時在互聯網上閱讀)。 – Tvd 2011-05-30 09:26:46
+1對於很好的答案 – 2012-04-30 08:06:17
它的工作。謝謝。但是我在一個活動中嵌套了片段。我如何處理後臺? – TheOnlyAnil 2015-09-22 15:10:44