我用sharedpreferences在我登錄活動,如果該值是 提供sharedpreference它會打開HomeActivityAndroid應用程序不關閉在backpress因爲sharedpreferences
但是,問題出現了,當我按下返回按鈕的應用程序去的到我的 以前的活動即。 loginActivity正在檢查 sharedpreference值以打開HomeActivity。這使我的應用程序 返回到HomeActivity。
LoginActivity.java代碼
@Override
protected void onResume() {
super.onResume();
//In onresume fetching value from sharedpreference
SharedPreferences sharedPreferences = getSharedPreferences(SharedPrefConfig.SHARED_PREF_NAME,Context.MODE_PRIVATE);
loggedIn = sharedPreferences.getBoolean(SharedPrefConfig.LOGGEDIN_SHARED_PREF, false);
username = sharedPreferences.getString(SharedPrefConfig.USERNAME_SHARED_PREF, null);
if(loggedIn){
//We will start the Home Activity
Intent intent = new Intent(this,HomeActivity.class);
startActivity(intent);
}
}
HomeActivity.java代碼
boolean doubleBackToExitPressedOnce = false;
@Override
public void onBackPressed() {
if (doubleBackToExitPressedOnce) {
super.onBackPressed();
return;
}
this.doubleBackToExitPressedOnce = true;
Toast.makeText(this, "Please click BACK again to exit", Toast.LENGTH_SHORT).show();
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
doubleBackToExitPressedOnce=false;
}
}, 2000);
}
set nohistory =「true」在清單文件中登錄Activity。 – Vishwa