2015-11-20 31 views
-1

我有一個應用程序,我已經把該選項登錄Facebook。用戶登錄後,我在共享首選項中存儲布爾值,以便下次用戶打開該應用程序時,他將被定向到主屏幕而不是登錄屏幕。應用程序要求登錄如果被殺死

我注意到一件事,如果我殺了應用程序,它會在下次打開它時要求登錄。它是android應用程序的默認行爲還是可以修復的,這樣我的應用程序即使在被殺死後也能保留登錄參數?

我正在使用TinyDB來存儲和檢索SharedPreferences中的值。

這裏是我的onCreate方法:

protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     tinyDB = new TinyDB(this); 
     final boolean loggedIn = tinyDB.getBoolean("Login", false); 
     if(loggedIn) { 
      Intent intent = new Intent(LaunchActivity.this, HomeActivity.class); 
      startActivity(intent); 
      LaunchActivity.this.finish(); 
     } 

     Fabric.with(this, new Crashlytics()); 
     FacebookSdk.sdkInitialize(getApplicationContext()); 
     setContentView(R.layout.activity_launch); 
     } 

     int SPLASH_TIME_OUT = 3000; 
     new Handler().postDelayed(new Runnable() { 
      /* 
      * Showing splash screen with a timer. This will be useful when you 
      * want to show case your app logo/company 
      */ 

      @Override 
      public void run() { 
       // This method will be executed once the timer is over 
       startLoginOrHome(loggedIn); 
      } 
     }, SPLASH_TIME_OUT); 
    } 

    private void startLoginOrHome(boolean loggedIn) { 
     if(loggedIn) { 
      LaunchActivity.this.finish(); 
     } else { 
      Intent intent = new Intent(LaunchActivity.this, SignInActivity.class); 
      startActivity(intent); 
      LaunchActivity.this.finish(); 
     } 
    } 
+0

是在'onCreate'登錄? –

+0

@RuchirBaronia是的,我在onCreate –

+0

檢查你得到的共享pref值的默認值是什麼 – Abx

回答

0

對我來說,你的問題是在拯救布爾值。 不需要使用TinyDB只需在登錄時添加以下代碼即可。

SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(); 
prefs.edit().putBoolean("login", true).commit(); 

和OnCreate中使用

SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(); 
Boolean isLoggedIn = prefs.getBoolean("login", false); 

Log.v(TAG,String.ValueOf(isLoggedIn)); //Use Logging to monitor 

    if(isLoggedIn) { 
     goToHomeScreen() 
    } 
else 
    goToLogInScreen() 
+0

我已經閱讀了TinyDB的代碼,在TinyDB中我沒有看到你寫的和TinyDB有什麼不同。效果是一樣的。請告訴我在我的代碼中導致問題的是什麼問題,因爲我使用的邏輯與您所​​寫的內容相同。 –

+0

使用日誌記錄來監視問題是否正在檢索isLoggedIn值。代碼看起來不錯 –

+0

當然,會嘗試。 –