简体   繁体   中英

How to save data using Shared Preferences in my code?

I am making an android app in which I am logging through Facebook and after accessing Facebook I am setting My navigation drawer with Facebook User Image Url and Facebook User Name. when I try to open my app second time the Facebook User Image Url and Facebook User Name doesn't come on navigation drawer.

I tried Using Shared Preferences to save the Facebook User Name and Image Url but I need help now because I tried well but it didn't happened.

    Bundle bundle = getIntent().getExtras();
    String id = bundle.getString("id");
    String name = bundle.getString("name");
    String image_url = "https://graph.facebook.com/" + id + "/picture?type=normal";

    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
    SharedPreferences.Editor editor = prefs.edit();
    editor.putString("name", name);
    editor.putString("url",image_url);
    editor.commit();

    final IProfile profile = new 
    ProfileDrawerItem().withName(prefs.getString("name",""))
            .withIcon(prefs.getString("url",""))
            .withIdentifier(100);

    AccountHeader headerResult = new AccountHeaderBuilder()
            .withActivity(this)
            .withTextColor(Color.BLACK)
            .withTranslucentStatusBar(true)
            .addProfiles(profile)
            .build();

The preferences code actually looks okay. So I suspect the problem actually lies somewhere else.

Have you actually tried logging the result of prefs.getString("url", "") or prefs.getString("name", "") to the console to check whether anything could be retrieved?

Your problem may even be from "higher up" in your code...have you actually verified that bundle.getString("id") and bundle.getString("name") return the data that you expect?

That being said, there are a couple of things you could do to improve readability and stability of your SharedPreferences code.

Declare the preferences as a class field.

public class YourActivity extends Activity {       

    private SharedPreferences userPreferences;


    @Override
    public void onCreate (Bundle savedInstanceState) {
        userPreferences = getSharedPreferences("USER_DATA", Context.MODE_PRIVATE);

        // ...
    }

    // ...
}

You don't need to keep a reference of the SharedPreferences.Editor instance because all put operations return one. So you can chain the calls:

userPreferences.edit()
        .putString("name", name)
        .putString("url", image_url)
        .commit();

If you can't get it to work like this, you can check the returned boolean of the chained call above. SharedPreferences.Editor::commit will actually return a boolean that signifies, whether the write was successfull ( true ) or failed ( false ).
In general though, I would advise to use apply() rather than commit() because it is non-blocking. Ie changes will be written to memory and then written to disk scheduled by the OS. The way it is right now the commit will block your UI thread, which may cause performance problems.

Your have to save data like this :

SharedPreferences prefs = getSharedPreferences("MyPrefs", MODE_PRIVATE)
SharedPreferences.Editor editor = prefs.edit();
editor.putString("name", name);
editor.putString("url",image_url);
editor.commit();

and to get your data :

SharedPreferences prefs = getSharedPreferences("MyPrefs", MODE_PRIVATE)
String name = perfs.getString("name","");
String url  = perfs.getString("url","");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM