Hi i have a inlogscreen (inlogdialog.xml), which includes 2 EditText (username, passwd) and i have a CheckBox (saveuser) with which the user can decide weather to save the username or not.
On the mainform (main.xml) i have a listner for these 3 values:
private class OnReadyListener implements MyCustomForm.ReadyListener {
public void ready(String user, String pass, boolean save) {
username = user;
password = pass;
}
}
Now i first want to save the username through SharedPreferences but it doesn`t get saved, can someone help me please?
If i check with system.out.println, the username is present in String username.
SharedPreferenes code in main.xml:
public static final String USERNM = "";
private SharedPreferences mPrefs;
.......
@Override
protected void onPause() {
Editor e = mPrefs.edit();
e.putString(USERNM, username); <----
e.commit();
Toast.makeText(this, "Items saved.", Toast.LENGTH_SHORT).show();
super.onPause();
}
Edit:
public static final String PREFS_NAME = "SharedPrefsDemoPreferences";
public static final String PREF_BOOL = "Bool";
public static final String USERNM = "User";
private SharedPreferences mPrefs;
....
@Override
protected void onResume() {
mPrefs = getSharedPreferences(PREFS_NAME, 0);
if(mPrefs!=null)
myBoxState=mPrefs.getBoolean(PREF_BOOL, false);
super.onResume();
}
Edit 2:
@Override
protected void onPause() {
System.out.println("user: " + username); <---- value username is there
Editor e = mPrefs.edit();
//e.putBoolean(PREF_BOOL, nieuwbel.isChecked());
e.putString(USERNM, username);
e.commit();
Toast.makeText(this, "Instelling beltegoed opgeslagen.", Toast.LENGTH_SHORT).show();
super.onPause();
}
Better not use empty key for the username. Saving data in SharedPreferences is based on saving a key-value pair. You retrieve the value by providing the key. Currently the key for your username is empty string.
Try something like:
Edit: This is how you retrieve from SharedPreferences:
IN your case this will look like:
than
returnedUsernameholds the value you stored in SharedPrefs.