Bossby
Bossby

Reputation: 49

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.Editor prefsEditor = mPrefs.edit();

    Gson gson = new Gson();
    String json = gson.toJson(userData);
    Log.d("data", " Setup --> "+json);
    prefsEditor.putString("userinfo", json);
    prefsEditor.commit();

}

Log.d result is like this

 Setup --> {"nameValuePairs":{"userData":{"nameValuePairs":{"phone":"089688xxxxxxx",
  "username":"username of User","flag":1,"Email":"[email protected]",
  "tipe":"TP001","Deskripsi":"Ini tentang gua","user_id":"USER001",
 "password":"c83e4046a7c5d3c4bf4c292e1e6ec681","fullname":My fullname"}},"status":"true"}}

then i'm trying to retrieve it, in other activity here is what i do

 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        SharedPreferences mPrefs = getPreferences(MODE_PRIVATE);
        SharedPreferences.Editor prefsEditor = mPrefs.edit();

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

    }

and here how i open my other activity

Intent intent = new Intent(Login.this, MainActivity.class);
startActivity(intent);

With my script above, the result from my logcat , i only see like Log.d above. So my question is, how can i retrieve it ?

Upvotes: 2

Views: 9097

Answers (5)

Ucdemir
Ucdemir

Reputation: 3098

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

Source

Upvotes: 0

Cao Minh Vu
Cao Minh Vu

Reputation: 1950

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

SharedPreferences mPrefs = getSharedPreferences("FILE_NAME",MODE_PRIVATE);
SharedPreferences.Editor prefsEditor = mPrefs.edit();

Gson gson = new Gson();
String json = 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);

    SharedPreferences mPrefs = getSharedPreferences("FILE_NAME",MODE_PRIVATE);
    SharedPreferences.Editor prefsEditor = mPrefs.edit();

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

}

Hope this help.

Upvotes: 0

Tenten Ponce
Tenten Ponce

Reputation: 2506

Try to add a key on your SharedPreferences:

public void StoreToshared(Object userData){
    SharedPreferences mPrefs = getSharedPreferences("your_sp_key", MODE_PRIVATE); //add key
    SharedPreferences.Editor prefsEditor = mPrefs.edit();

    Gson gson = new Gson();
    String json = gson.toJson(userData);
    Log.d("data", " Setup --> "+json);
    prefsEditor.putString("userinfo", json);
    prefsEditor.commit();

}

Retrieval:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

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

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

}

Upvotes: 2

Thientvse
Thientvse

Reputation: 1791

You can create 2 method:

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

and

// get value
public static String getPref(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

String data = getPref("userinfo", getApplicationContext());

I hope it can help your problem!

Upvotes: 1

Kartik Arora
Kartik Arora

Reputation: 571

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.

Upvotes: 0

Related Questions