2017-01-12 26 views
-4

進行登錄:如果應用程序是登錄及其背景,然後點擊應用啓動器圖標從主屏幕上會顯示登錄屏幕

SharedPreferences sharedPref2 = getSharedPreferences("", Context.MODE_PRIVATE); 

String userid = sharedPref2.getString(SharedPref.USER_ID, user_id); 
if (userid != null) { 
    Intent i = new Intent(LoginActivity.this, HomeActivity.class); 
    startActivity(i); 
    return; 
} 

如果應用程序是從後臺打開,然後它會顯示HomeActivity正常, 但當應用程序在後臺,並從啓動器圖標打開它,然後只顯示登錄屏幕。

+0

登錄後是否保存了用戶名? –

+0

請用一些清晰的想法編輯您的問題 –

+0

@VivekMishra yes用戶標識已保存。 – Mayuri

回答

0

將整個代碼放入一個函數中,並檢查用戶是否已登錄或未登錄到loginActivity的onCreate方法中。

@Override 
onCreate(Bundle savedBundleState){ 
..... 

if(isUserloggedIn()){ 
Intent i = new Intent(LoginActivity.this, HomeActivity.class); 
    startActivity(i); 
    finish(); 
    } 

} 


boolean isUserloggedIn(){ 
     String userid = sharedPref2.getString(SharedPref.USER_ID, user_id); 
if (userid != null) { 
    return true; 
} 
else{ 
return false;} 
} 
+0

非常感謝....它適用於我.. – Mayuri

+0

鼓勵幫助upvote和接受答案,如果你不介意:) –