I want to check the line in SharedPreferences, which used it to store the username and password, so if the username and password are not null or empty, they will be redirected to the home page, otherwise, if the username and password are empty, it will be sent for login page
This is my code to check the SharedPreferences line, but it does not work.
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 verify this through a toast using this code, and after I tried this, I got a SharedPreferences string that 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 empty or null?
Thanks..
Christian wibowo
source share