我想检查SharedPreferences中的一个字符串,用它来存储用户名和密码,所以如果用户名和密码不为空或空,它将被定向到home,否则如果用户名和密码为空,它将被定向到登录页面.
这是我检查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);
}
我尝试通过这个代码的吐司检查它,并在我尝试这个之后,我得到SharedPreferences字符串不为null或空..
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字符串是空还是空?
谢谢..