在按下後退按鈕時,我試圖從activity中退出應用程序。但問題出在按後退鍵刷新同樣的活動2到3次之後,進入Login活動並且不會退出應用程序。可以有人幫助解決問題。如何退出應用程序的活動在按下按鈕在android中?
這是我如何試圖退出應用程序。
@Override
public void onBackPressed() {
super.onBackPressed();
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
final SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putBoolean("LISTVIEW_EVENT_ONE", false);
editor.putBoolean("LISTVIEW_EVENT_TWO", false);
editor.commit();
this.finish();
//Intent startMain = new Intent(Intent.ACTION_MAIN);
//startMain.addCategory(Intent.CATEGORY_HOME);
//startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
/*Intent startMain = new Intent(Intent.ACTION_MAIN);
startMain.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(startMain);*/
/* Intent a = new Intent(Intent.ACTION_MAIN);
a.addCategory(Intent.CATEGORY_HOME);
a.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(a);*/
}
android.os.Process.killProcess(android.os.Process.myPid()); – tianwei
完成你的活動'onBackPressed()'不是一個好主意。當你開始這個活動時完成你的登錄活動。一旦你這樣做,正常的後退將關閉應用程序。 – Rohit5k2
返回將彈出backstack,如果你沒有任何活動在後面的堆棧它會關閉應用程序;只有在你知道自己在做什麼時才使用kill流程 – for3st