Brahadeesh
Brahadeesh

Reputation: 2255

Shared Preferences android basic question

I save a user's username and passwords the first time he opens the app and store it in a SharedPreferences object. I check for the data the second time he enters and if its not null, then I got into the app. Here is how I'm doing this:

private SharedPreferences dhj;
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    dhj = this.getSharedPreferences("DHJ", MODE_WORLD_READABLE);
    if(dhj.getString("username", null) != null) {
        setContentView(R.layout.main);
            // do some stuff...
    }
    else {
            setContentView(R.layout.login);
            username = (EditText) findViewById(R.id.username);
            password = (EditText) findViewById(R.id.password);
                    loginButton = (Button) findViewById(R.id.loginButton);

            loginButton.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    SharedPreferences.Editor dhjEditor = dhj.edit();
                    dhjEditor.putString("username", username.getText().toString());
                    dhjEditor.putString("password", password.getText().toString());
                    setContentView(R.layout.main);
                }
            }); 
                    // do some other stuff...
    }
}

But each time I open the app, I am being asked to enter the username and password.
What am I doing wrong? How can I achieve the desired functionality?
Thank you.

Upvotes: 2

Views: 3613

Answers (3)

dbm
dbm

Reputation: 10485

Note that the editor.commit() function is a synchronous function that performs a file system operation. Calling this from the main thread (your code seems to run in the main thread) might - in unfortunet situations - throw a ANR since file system operations might stall and thereby block the main thread.

I would use the editor.apply() function instead, since it will immediately update the in-memory cache of your shared preferences and then create a worker thread and write the values to your shared preferences file from there (worker threads don't block the main thread).

http://developer.android.com/reference/android/content/SharedPreferences.Editor.html#apply()

Upvotes: 3

didi_X8
didi_X8

Reputation: 5068

The doc of "getSharedPreferences" says:

Desired preferences file. If a preferences file by this name does not exist, it will be created when you retrieve an editor (SharedPreferences.edit()) and then commit changes (Editor.commit()).

Make sure you use the same editor for all writing before committing, e.g.

Editor editor = mPref.edit();
editor.putString("username", username);
editor.putString("password", password);
editor.commit();

Upvotes: 2

james
james

Reputation: 26271

You need to call the Editor's commit method after making any change to preferences. This will save the preferences file:

SharedPreferences.Editor dhjEditor = dhj.edit();
dhjEditor.putString("username", username.getText().toString());
dhjEditor.putString("password", password.getText().toString());
dhjEditor.commit();

Upvotes: 1

Related Questions