检查android studio

时间:2017-01-16 22:53:33

标签: java android

如果共享首选项存在,我将登录数据保存在共享首选项值集中以跳过登录。

我在此处使用此代码保存此数据:



//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();




如果共享首选项loginPrefs存在,我想在登录活动开始时查看。

我该怎么做?有没有办法问:



//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
}




感谢您帮助我:)

1 个答案:

答案 0 :(得分:1)

使用此:

//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
}