[英]Check if shared preferences exist in android studio
I'm saving login data in a shared preferences value set to skip the login if the shared preferences exists. 我将登录数据保存在共享首选项值中,如果存在共享首选项,该值将跳过登录。
I'm saving this data with this code here: 我使用以下代码保存此数据:
//Save userdata as SharedPreferences
SharedPreferences loginPreferences = getSharedPreferences("loginPrefs", MODE_PRIVATE);
SharedPreferences.Editor loginEditor = loginPreferences.edit();
loginEditor.putString("displayName", displayName);
loginEditor.putString("loginEmail", loginEmail);
loginEditor.putString("loginPassword", loginPassword);
loginEditor.commit();
Finaly I want to check at the start of my login activity if the shared preferences loginPrefs
exist. 最后,我想在登录活动开始时检查是否存在共享首选项loginPrefs
。
How can I do this? 我怎样才能做到这一点? Is there a way to ask: 有没有办法问:
//Pseudocode
if (SharedPreferences loginPrefs exists) { //How can I ask here?
Intent intentMain = new Intent(LoginActivity.this, NavigationDrawerActivity.class);
LoginActivity.this.startActivity(intentMain);
finish();
} else {
//Set the shared preferences
}
Use this: 用这个:
//Pseudocode
SharedPreferences loginPreferences = getSharedPreferences("loginPrefs", MODE_PRIVATE);
if (loginPreferences.contains("loginEmail")) { //How can I ask here?
Intent intentMain = new Intent(LoginActivity.this, NavigationDrawerActivity.class);
LoginActivity.this.startActivity(intentMain);
finish();
} else {
//Set the shared preferences
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.