I have two textbox and a checkmebox. But when i type my username and password and tick on the checkmebox, exit the application and go back again it doesn’t appear. Why?
// Get reference to UI elements
txtLogin = (EditText) findViewById(R.id.txtLogin);
txtPassword = (EditText) findViewById(R.id.txtPassword);
SharedPreferences pref = getSharedPreferences(PREFS_NAME,MODE_PRIVATE);
String username = pref.getString(PREF_USERNAME, null);
String password = pref.getString(PREF_PASSWORD, null);
if (username == null || password == null) {
//Prompt for username and password
Toast.makeText(getBaseContext(),
"HI",
Toast.LENGTH_SHORT).show();
}
// Remember me function
CheckBox cbRemember = (CheckBox) findViewById(R.id.chkRememberPassword);
if (cbRemember.isChecked()) {
getSharedPreferences(PREFS_NAME,MODE_PRIVATE)
.edit()
.putString(PREF_USERNAME, txtLogin.toString())
.putString(PREF_PASSWORD, txtPassword.toString())
.commit();
}
First: You should not be using
txtLogin.toString();but rather usetxtLogin.getText();to get the values from the editText controls.Are you sure the code that saves the preferences is invoked?