I want to check a string in SharedPreferences used it to store username and password, so if username and password is not null or empty it will be directed to home, otherwise if the username and password is empty it will be directed to login page.
This is my code to check the SharedPreferences string, but it is not working..
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);
}
I tried to check it through toast with this code, and after I tried this, I get SharedPreferences string is not null or empty..
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();
}
}
});
How can I correctly check the SharedPreferences string whether its empty or null?
Thanks..
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…