SharedPreferences持续返回默认值:注册登录后无法传递姓名与用户类型
Hey there, let's work through why your SharedPreferences is returning empty values after registration and login. I've tackled this exact issue a bunch of times, so here's what to check and fix step by step:
1. 先排查注册页的存储逻辑(最容易踩坑的地方)
Most of the time, the problem is that the data never actually gets saved to SharedPreferences. Here's what you need to verify:
- Don't forget to commit the editor changes: You must call
apply()orcommit()after adding values to the editor—without this, nothing gets written to storage. - Ensure matching keys: The key you use to store data (e.g.,
"user_name") must be identical to the key you use to read it later (no typos, case differences, or extra spaces!). - Use consistent SharedPreferences name: Stick to the same preference file name across all activities (like
"UserProfilePrefs").
注册页正确存储示例代码:
// 注册成功后,获取用户输入的姓名和类型 String inputName = etUserName.getText().toString().trim(); String inputType = etUserType.getText().toString().trim(); // 存储到SharedPreferences SharedPreferences prefs = getSharedPreferences("UserProfilePrefs", Context.MODE_PRIVATE); SharedPreferences.Editor editor = prefs.edit(); editor.putString("user_name", inputName); editor.putString("user_type", inputType); editor.apply(); // 关键!一定要调用这个来提交修改 // 跳转到登录页 Intent loginIntent = new Intent(RegisterActivity.this, LoginActivity.class); startActivity(loginIntent); finish();
2. 登录页无需额外传值,直接跳转即可
Since SharedPreferences is a global storage solution, you don't need to pass data via Intent extras from login to home. Just make sure you only navigate to the home page after a successful login check.
3. 首页读取逻辑要对应
In your home Activity, read the values using the same preference file name and keys you used in the registration page. Add default values to avoid null crashes, and add debug logs to verify what's being read.
首页读取示例代码:
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_home); TextView tvDisplayName = findViewById(R.id.tv_display_name); TextView tvDisplayType = findViewById(R.id.tv_display_type); // 读取SharedPreferences SharedPreferences prefs = getSharedPreferences("UserProfilePrefs", Context.MODE_PRIVATE); String userName = prefs.getString("user_name", ""); // 空字符串作为默认值 String userType = prefs.getString("user_type", ""); // 调试用Log,看读取到的内容 Log.d("HomeActivity", "Read name: " + userName + ", Type: " + userType); // 显示到界面 if (!userName.isEmpty()) { tvDisplayName.setText("姓名:" + userName); } else { tvDisplayName.setText("未获取到用户姓名"); // 排查用提示 } if (!userType.isEmpty()) { tvDisplayType.setText("用户类型:" + userType); } else { tvDisplayType.setText("未获取到用户类型"); } }
额外排查点
- Check for accidental clears: Make sure no code in your registration or login flow is calling
editor.clear().apply()(this would wipe all saved data). - Validate user input: Add checks in the registration page to ensure the user actually entered a name and type before saving—empty input will result in empty values being stored.
- Context consistency: Using
getApplicationContext()instead of the Activity context is fine, but ensure you're using the same context type (or just stick tothisfor Activity-level access, it works perfectly).
内容的提问来源于stack exchange,提问作者ramen05

