Save user and password Android

303 views Asked by At

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?

5

There are 5 answers

0
hemanth1488 On

You can use Saved Preference to save the User. We can do this using gson.jar

 SharedPreferences  mPrefs = getPreferences(MODE_PRIVATE);

For saving

 Editor prefsEditor = mPrefs.edit();
 Gson gson = new Gson();
 String json = gson.toJson("User");
 prefsEditor.putString("MyObject", json);
 prefsEditor.commit();

For getting

Gson gson = new Gson();
String json = mPrefs.getString("User", "");
MyObject obj = gson.fromJson(json, User.class);
0
Thinsky On

Because you save the user in the memory, and you need to save it in the SharedPreference.

0
Amsheer On

The reason you are not getting value is your data not stored. Once the Application is closed your static data will be lost. You need to save the data in Database or SharedPreference.

Your model is just a static method saving data temporarily.

0
Bharatesh On

It all depends how much data it is (other than user id and password)

As @Gosu said if you have small data use SharedPrefereces or if have more number of recrods and tables then use SQLite

For more refer this : Official Site : Android Storage Options

1
Bruce On

If your constraint is only username and password (not user roles & permissions) Then best way without much work is SharedPreferences. Else you need have a database like sqlite maintain these informations.