我想在SharedPreferences中检查一个字符串,该字符串用于存储用户名和密码,如果用户名和密码不为空,则将其重定向到主页,否则,如果用户名和密码为空,则将其重定向到登录页面。
这是我检查SharedPreferences字符串的代码,但它无法正常工作...
if(PreferenceConnector.USERNAME!=null){
Intent intent = new Intent(MainActivity.this,MainHome_Activity.class);
startActivity(intent);
} else {
Intent intent = new Intent(MainActivity.this,LoginFormPegawai_Activity.class);
startActivity(intent);
}
我尝试使用这段代码通过 Toast 进行检查,尝试后,我得到了 SharedPreferences 字符串不为空的结果。
btn_logout_pegawai.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
//remove the SharedPreferences string
PreferenceConnector.getEditor(this).remove(PreferenceConnector.USERNAME)
.commit();
PreferenceConnector.getEditor(this).remove(PreferenceConnector.PASSWORD)
.commit();
//checking the SharedPreferences string
if(PreferenceConnector.USERNAME!=null){
Toast.makeText(MainHome_Activity.this,"not null", Toast.LENGTH_SHORT).show();
}
else {
Toast.makeText(MainHome_Activity.this,"null", Toast.LENGTH_SHORT).show();
}
}
});
我该如何正确地检查SharedPreferences字符串是否为空或为null?
谢谢。