[英]Adding two values from two different activities through Shared Preferences
[英]Not able to read/write data properly from shared preferences from different activities
我正在尝试检查用户是否已经使用共享首选项登录到我的android应用程序。
我有一个闪屏,尝试从共享的首选项中读取userID,如果不存在该userID,我将启动登录活动,否则将重定向到home活动。
闪屏:
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
final int vendorId = prefs.getInt("vendorId", 0);
if (0 == vendorId) {
/* New Handler to start the Menu-Activity
* and close this Splash-Screen after some seconds.*/
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
/* Create an Intent that will start the Menu-Activity. */
Intent mainIntent = new Intent(WelcomeFlashScreen.this, LoginActivity.class);
WelcomeFlashScreen.this.startActivity(mainIntent);
WelcomeFlashScreen.this.finish();
}
}, SPLASH_DISPLAY_LENGTH);
}
else
{
/* New Handler to start the Menu-Activity
* and close this Splash-Screen after some seconds.*/
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
/* Create an Intent that will start the Menu-Activity. */
Intent mainIntent = new Intent(WelcomeFlashScreen.this, HomeActivity.class);
mainIntent.putExtra(Constants.VENDOR_ID,vendorId);
WelcomeFlashScreen.this.startActivity(mainIntent);
WelcomeFlashScreen.this.finish();
}
}, SPLASH_DISPLAY_LENGTH);
}
}
在登录活动中,如果登录成功,我将使用以下代码将数据填充为共享首选项。
if ("success".equals(status)&&"".equals(errorCode)) {
String vendorId = rootJsonObject.get("data").getAsJsonObject().get("id").getAsString();
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
prefs.edit().putString(Constants.VENDOR_ID, vendorId).commit();
loginSucces();
}
我调试了代码,发现从首选项读取/写入时没有异常,但是即使将数据放入loginActivity后,我也无法从闪存屏幕活动中读取数据。
提前致谢。
您正在使用putString(...)编写字符串,但尝试使用getInt(...)将其作为Int读取
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.