Skip to content Skip to sidebar Skip to footer

How To Get Value Of Sharedpreferences Android

I'm trying to use SharedPreferences here is what i do public void StoreToshared(Object userData){ SharedPreferences mPrefs = getPreferences(MODE_PRIVATE); SharedPreferences

Solution 1:

Try to add a key on your SharedPreferences:

publicvoidStoreToshared(Object userData){
    SharedPreferencesmPrefs= getSharedPreferences("your_sp_key", MODE_PRIVATE); //add key
    SharedPreferences.EditorprefsEditor= mPrefs.edit();

    Gsongson=newGson();
    Stringjson= gson.toJson(userData);
    Log.d("data", " Setup --> "+json);
    prefsEditor.putString("userinfo", json);
    prefsEditor.commit();

}

Retrieval:

@OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    SharedPreferencesmPrefs= getSharedPreferences("your_sp_key", MODE_PRIVATE); //add key
    SharedPreferences.EditorprefsEditor= mPrefs.edit();

    Stringdata= mPrefs.getString("userinfo", null);
    Log.i("Text", "Here is the retrieve");
    Log.i("data", " retrieve --> "+data);

}

Solution 2:

You can create 2 method:

// put valuepublicstaticvoidputPref(String key, String value, Context context) {
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
    SharedPreferences.Editor editor = prefs.edit();
    editor.putString(key, value);
    editor.commit();
}

and

// get valuepublicstaticStringgetPref(String key, Context context) {
    SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
    return preferences.getString(key, null);
}

then you can put value

putPref("userinfo", "/** user data json */", getApplicationContext());

and get value

Stringdata= getPref("userinfo", getApplicationContext());

I hope it can help your problem!

Solution 3:

You need to convert the string data from SharedPreferences back to a PoJo using Gson. Simply do this:

Object userData = new Gson().fromJson(data, Object.class);

I guess that should solve it.

Solution 4:

The api: getPreferences will use the activity name to create an xml file if not exists. For example, assume StoreToshared method is put in activity: LoginActivity.java, it will create a file: LoginActivity.xml to store your pref data. Hence, when you go into other activity, let say its name is: MainActivity.java, getPreferences will look into file "MainActivity.xml" instead of "LoginActivity.xml", that is why you cannot retrieve your data.

The solution is to use: getSharedPreferences. Hence your code can be modified as follow:

public void StoreToshared(Object userData) {

SharedPreferencesmPrefs= getSharedPreferences("FILE_NAME",MODE_PRIVATE);
SharedPreferences.EditorprefsEditor= mPrefs.edit();

Gsongson=newGson();
Stringjson= gson.toJson(userData);
Log.d("data", " Setup --> "+json);
prefsEditor.putString("userinfo", json);
prefsEditor.commit();

}

@Override protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    SharedPreferencesmPrefs= getSharedPreferences("FILE_NAME",MODE_PRIVATE);
    SharedPreferences.EditorprefsEditor= mPrefs.edit();

    Stringdata= mPrefs.getString("userinfo", null);
    Log.i("Text", "Here is the retrieve");
    Log.i("data", " retrieve --> "+data);

}

Hope this help.

Solution 5:

In year 2020, Google has been released new data storage that is repleced of Shared Preference... It' is developed with "Kotlin"

Source

Post a Comment for "How To Get Value Of Sharedpreferences Android"