开发者

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.


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()


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();


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();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜