This topic How to save a custom Java Object in Android? is what comes near enough for my solution but without success. So I have a custom object, that I would like to add to a Favorite list, which means that I would like to find my saved object anywhere in the app (all activities). But I don't know how to call in my activity (where is the favorite list) my shared preferences created in another activity, to after get back the json.
Asked
Active
Viewed 1,145 times
1
-
I believe Share Preferences you can access in different activity if you use default one.. or specific name.. – ValayPatel Apr 13 '16 at 13:53
-
Try to convert your custom object to a string. And save the string via the PreferencesManager. – Luca Ziegler Apr 13 '16 at 13:57
-
@LucaZiegler what then ? – Jay Apr 13 '16 at 14:00
-
@ValayPatel, i tried to didnt work :/ – Jay Apr 13 '16 at 14:01
-
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext()); will give you default SP. Use GSON to convert your custom object to JSON string and store it as string.. Later fetch the string and using GSON convert that to Custom object of yours. – ValayPatel Apr 13 '16 at 14:02
-
This might be helpful http://stackoverflow.com/questions/20819294/save-custom-object-array-to-shared-preferences – ValayPatel Apr 13 '16 at 14:04
-
@ValayPatel i tried it and it doesnt recognize mPrefs... his example must be in the same class – Jay Apr 13 '16 at 14:12
-
Can you put you whole code in example..Where mPrefs is defined?? – ValayPatel Apr 13 '16 at 14:15
-
Thanks for the explanation, this is what i did: addFav.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { SharedPreferences.Editor prefsEditor = mPrefs.edit(); Gson gson = new Gson(); String myJson = gson.toJson(currentProduit); prefsEditor.putString("myproduct", myJson); prefsEditor.apply(); } }); after that i dont know how to get it back in other class – Jay Apr 13 '16 at 14:15
-
This is my code for the shared preferences in one class SharedPreferences mPrefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext()); SharedPreferences.Editor prefsEditor = mPrefs.edit(); Gson gson = new Gson(); String myJson = gson.toJson(currentProduit); prefsEditor.putString("myproduct", myJson); prefsEditor.apply(); After that i dont know what to put in the other class to get back that data – Jay Apr 13 '16 at 14:18
-
In other class -->SharedPreferences mPrefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());String myJson= mPrefs.getString("myproduct") and than convert the myJSON string to your object using toObject method – ValayPatel Apr 13 '16 at 14:25
-
@ValayPatel this line seems to require two strings String myJson = mPrefs.getString("myproduct", //missing?); – Jay Apr 13 '16 at 14:32
-
Thats the default value you have provide.. If there is no shared preference available.. Just put null for the time being and thing about the default value later on. – ValayPatel Apr 13 '16 at 14:34
-
@ValayPatel thanks ! It worked, just had a question, can I store multiple objects my sharedpreferences ? EDIT: dont know if its possible to accept ur answer because its a comment ? (newbie to stack :D) – Jay Apr 13 '16 at 14:49
-
As much as you want.. Don't forget to up vote answer.. – ValayPatel Apr 13 '16 at 14:49
-
@ValayPatel your answer is a comment, there is not the tick to put in green to accept ur answer :/ – Jay Apr 13 '16 at 14:53
-
@Jay added answer :) – ValayPatel Apr 13 '16 at 15:13
3 Answers
4
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
will give you default SP. Use GSON to convert your custom object to JSON string and store it as string.. Later fetch the string and using GSON convert that to Custom object of yours.
In other class
SharedPreferences mPrefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
String myJson= mPrefs.getString("myproduct")
and than convert the myJSON string to your object using toObject method

ValayPatel
- 1,094
- 12
- 15
0
Follow the android documentation on shared preference it contains details on how to create shared preference accessible in all activities Android Documentaion on shared preference

kofoworola
- 557
- 1
- 5
- 14
0
You can save your data as key-value in storage by using (Hawk library) - you can save all data types and objects then retrieve it at any time. the link on GitHub Hawk Documentation * your data will be encrypted in the storage
to save data
Hawk.init(context).build();
Hawk.put("key", "value");
to retrieve data
Hawk.init(context).build();
String s = Hawk.get("key");

Ahmed Abdalla
- 2,356
- 2
- 14
- 27