2017-04-11 41 views
0

我用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); 
} 
+0

set nohistory =「true」在清單文件中登錄Activity。 – Vishwa

回答

0

請登錄完成活動時,意圖呼叫的裝置 添加此

Intent intent = new Intent(this,HomeActivity.class); 
       startActivity(intent); 
       finish(); 
0

當你點擊註銷然後寫這個代碼。所以它會將您重定向到LoginActivity。

Intent logoutIntent = new Intent(HomeActivity.this, LoginActivity.class); 
          logoutIntent.putExtra("isFromLogout", true); 
          logoutIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK); 
          startActivity(logoutIntent); 
          finish(); 

而在LoginActivity中寫下面的代碼。

boolean isFromLogout = getIntent().getBooleanExtra("isFromLogout", false); 
     if (!isTaskRoot() && !isFromLogout) { 
      finish(); 
      return; 
     } 

上面的情景會幫助你,如果你想從你的應用程序註銷。現在,當你在你的主頁上,當你按下返回按鈕,你想在後臺關閉你的應用程序,所以使用下面的代碼。

private void minimizeApp() { 
     Intent startMain = new Intent(Intent.ACTION_MAIN); 
     startMain.addCategory(Intent.CATEGORY_HOME); 
     startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
     startActivity(startMain); 
    } 
0

您的應用程序應該如何工作?

  1. 關閉後關閉應用程序HomeActivity

最簡單的方法是在開始家庭活動後完成登錄活動。

Intent intent = new Intent(this, HomeActivity.class); 
startActivity(intent); 
finish(); 
  • HomeActivity顯示登錄活動之後。
  • 你可以清除SharedPreferencesonDestroy方法HomeActivity類。所以登錄活動的onResume什麼都不要,什麼也不開。

    另一種方法 - 檢查HomeActivity結果。爲了這個需要改變開放代碼 - 地方startActivityForResult方法調用。

    Intent intent = new Intent(this, HomeActivity.class); 
    startActivityForResult(intent, 0); 
    

    然後在登錄活動的onActivityResult方法作出禁止HomeActivity開放(例如,再次清潔SharedPreferences)的一些行動。

    -1

    使用finish()方法。它會破壞活動。

    Intent intent = new Intent(this, HomeActivity.class);  
    startActivity(intent); 
    finish(); 
    
    相關問題