1

I know this is a well-known question (for example: How do you save/store objects in SharedPreferences on Android?) , but I was wondering if it was better to store an object in shared preferences (using json) or as a static object of an activity (companion object). This is an object that I will use it in almost all the activities and fragments.

For example, I have an User object.

The first solution will look like this:

val mUser = User(x,x,x...)
val mUserJson = Gson().toJson(mUser)

mSharedPreferences
                .edit()
                .putString(USER_DATA, mUserJson)
                .apply()

And for the second one, I declared the object on the first activity, and then on another activity I initialized it.

class FirstActivity : AppCompatActivity() {
    companion object {
        lateinit var mUser: User
    }

}

On another activity:

FirstActivity.mUser = User(x,x,x...)

I don't know if there is a problem with the second solution (efficiency?) but as for the first solution I have to initialized shared preferences for each activity that I'm willing to use the User object (is this too bad?). Also, if I want to change a User variable, I have to reconvert the user object into json and rewrite this preference:

val mUserPref = mSharedPreferences.getString(USER_DATA, "")
            .run { Gson().fromJson(this, User::class.java) }

mUserPref.variable = new_value
mSharedPreferences
                   .edit()
                   .putString(USER_DATA, Gson().toJson(mUserPref)) 
                   .apply()

So, can someone explain me which is the best solution and why? Thank you :)

ladytoky0
  • 588
  • 6
  • 16
  • `if it was better to store an object in shared preferences (using json) or as a static object of an activity (companion object)` these storages aren't the same thing, because the one is a only available for the duration of the app. you probably shouldn't even be using sharedPrefs for something like this and rather make use of a proper database, people overuse shared prefs because it's easy to use, but it doesn't make it the best tool for the job – a_local_nobody Nov 15 '21 at 15:03
  • @a_local_nobody how you would use/create a database to only store one object? – ladytoky0 Nov 15 '21 at 15:13
  • `how you would use/create a database to only store one object?` lots of resources available online for this and storing a single or multiple values would makes no difference, Room makes storing data easy, point is, do you actually _need_ persistent storage or not ? – a_local_nobody Nov 15 '21 at 16:12
  • @a_local_nobody it's only for the duration of the app, room is a good solution for this? – ladytoky0 Nov 15 '21 at 16:22
  • if it's only for the duration of the app, you should probably get rid of shared prefs and just use a companion object or something similar – a_local_nobody Nov 15 '21 at 16:23
  • If possible [Datastore](https://developer.android.com/topic/libraries/architecture/datastore) will help. – Anshul Nov 15 '21 at 17:04

0 Answers0