We need to get user objects in many places, which contain many fields. After login, I want to save/store these user objects. How can we implement this kind of scenario?
I can't store it like this:
SharedPreferences.Editor prefsEditor = myPrefs.edit();
prefsEditor.putString("BusinessUnit", strBusinessUnit);
Better is to Make a global
Constants
class to save key or variables to fetch or save data.To save data call this method to save data from every where.
Use it to get data.
and a method something like this will do the trick
You haven't stated what you do with the
prefsEditor
object after this, but in order to persist the preference data, you also need to use:My utils class for save list to
SharedPreferences
Using
.
Full code of my utils // check using example in Activity code
An other way to save and restore an object from android sharedpreferences without using the Json format
To add to @MuhammadAamirALi's answer, you can use Gson to save and retrieve a list of objects
Save List of user-defined objects to SharedPreferences
Get List of user-defined objects from SharedPreferences