this is my logic :
I have a user :
user = new User(newUsername, newPassword);
Random id + password are generated for the user.
uniqueName = UUID.randomUUID().toString();
uniquePassword = UUID.randomUUID().toString();
With a dialog the user can change his password, that happens like follows.
newUsername = userField.getText().toString();
newPassword = passwordField.getText().toString();
I pass it to the model :
user.setUsername(newUsername);
user.setPassword(newPassword);
Model looks like this :
public User(String username,String password){
this.username = username;
this.password = password;
}
public String getUsername(){
return username;
}
public void setUsername(String username){
this.username = username;
}
public String getPassword(){
return password;
}
public void setPassword(String password){
this.password = password;
}
However when i shut the application or change activites the password and user is not saved, any hint for how to save user and password so that if user leaves activity it saves the password/id?
I added SharedPreferences in this way :
private void savePreferences(){
SharedPreferences settings = getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = settings.edit();
editor.putString(PREF_UNAME, newUsername);
editor.putString(PREF_PASSWORD, newPassword);
editor.commit();
}
private void loadPreferences(){
SharedPreferences settings = getSharedPreferences(PREFS_NAME,
Context.MODE_PRIVATE);
mUsername.setText(settings.getString(PREF_UNAME, newUsername));
mPassword.setText(settings.getString(PREF_PASSWORD, newPassword));
}
I call savePreferences in onPause() and loadPreferences in onResume(). However when i close the activity two times and open it again the username and id i stored are gone. Any more hints?
You can use Saved Preference to save the User. We can do this using gson.jar
For saving
For getting