Android应用登录状态保持异常:重启后需重登的问题排查咨询
Hey there! Let's break down what's happening here. The core issue is that your app is always launching directly into the LoginActivity without checking if the user is already logged in first. Your SharedPrefManager is correctly storing the user session when they log in, but there's no logic to validate that session on app startup.
What's Missing?
Right now, when you restart the app, it opens LoginActivity by default (since it's probably set as the launcher activity in your AndroidManifest.xml). There's no check in place to see if a valid user session exists in SharedPreferences—so it just shows the login screen every time, even if the user never logged out.
Step-by-Step Fix
1. Add Login Status Check to Your Launcher Activity
You have two good options here: either modify your existing LoginActivity to check the session on startup, or create a simple SplashActivity as your launcher to handle the redirect.
Option A: Modify LoginActivity's onCreate Method
Update your LoginActivity's onCreate method to first check if the user is already logged in. If they are, skip the login screen and go straight to MainActivity:
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_login); // Check if user is already logged in if (SharedPrefManager.getInstance(this).isLoggedIn()) { // User is logged in, redirect to MainActivity finish(); startActivity(new Intent(this, MainActivity.class)); return; // Exit onCreate to avoid loading login UI } // Rest of your existing onCreate code (initializing views, etc.) }
Option B: Create a SplashActivity (Cleaner Approach)
If you want to avoid cluttering your LoginActivity, create a new SplashActivity that acts as the launcher. This activity will only handle the session check and redirect:
public class SplashActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_splash); // Add a small delay if you want a splash screen to show briefly new Handler(Looper.getMainLooper()).postDelayed(() -> { SharedPrefManager prefManager = SharedPrefManager.getInstance(this); Intent intent; if (prefManager.isLoggedIn()) { intent = new Intent(this, MainActivity.class); } else { intent = new Intent(this, LoginActivity.class); } startActivity(intent); finish(); // Close splash activity so user can't go back to it }, 1000); // 1-second delay (adjust as needed) } }
Then, update your AndroidManifest.xml to set SplashActivity as the launcher instead of LoginActivity:
<activity android:name=".SplashActivity" android:theme="@style/Theme.App.NoActionBar"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name=".LoginActivity" /> <activity android:name=".MainActivity" />
2. Verify Your SharedPrefManager Logic
Quick check: Your isLoggedIn() method looks correct—it checks if KEY_PHONE exists in SharedPreferences, which is set when the user logs in via userLogin(). Your logout method also correctly clears all preferences and redirects to LoginActivity, which is exactly what you want.
Additional Tips
- Make sure you call
finish()after starting MainActivity from LoginActivity/SplashActivity, so the user can't press the back button to return to the login screen without logging out. - If you're using the SplashActivity approach, keep it lightweight—don't add heavy operations here, just the session check and redirect.
That should fix your issue! Now when the user restarts the app, it will automatically take them to MainActivity if they're still logged in, and only show the login screen if they've logged out or are using the app for the first time.
内容的提问来源于stack exchange,提问作者Arpan Sarkar

