1

I am not sure of the best way to do this, when user logs in my app i want to store his data from an api. I will be using this data trough out the app, so i dont call getUserData all the time . So i want to ask for an opinion

Do i store user data in application context so i can access it in all activites/fragments

Or

Do i store the user data in SharedPreferences as json string (User has a lot of small data name,lastname,age,email...)

Or

Do i store it in database (Since i only need storing for this i tought this way might be too much, but could be wrong)

Or

There might be a better way to do this?

MePo
  • 1,044
  • 2
  • 23
  • 48
  • 1
    You should use sharedPreferences. – Ankita Sep 22 '17 at 05:41
  • refer https://stackoverflow.com/q/46356961/1848157 – Radhey Sep 22 '17 at 05:45
  • User shared preference. – Andy Developer Sep 22 '17 at 05:47
  • @Radhey i didn't mean like that i have getUserData method in API and that returns me his data. that is why i said "session" sorry if that made it confusing – MePo Sep 22 '17 at 05:49
  • Ok thank you guys, i will store user data in sharedPreferences. I was very unsure about this since it can be done in many ways – MePo Sep 22 '17 at 05:54
  • 1
    at the end you want to stores user data coming form the API right! you can write user data in to #file or you can use preferences. you have to mange it with flags as per your session and overwrite that data every time. – Radhey Sep 22 '17 at 05:54
  • @Radhey thank you i didn't think of storing in file. Will try preferences for start thank you – MePo Sep 22 '17 at 05:56
  • 1
    I create a singleton when the app is launched. It stores cookies in SharedPreferences.. – Rose Sep 22 '17 at 06:45

1 Answers1

1

SharePreferences are great, but it is stored in plain text and can be read by rooted devices. Someone with a stolen rooted phone can potentially access tons of user info.

Use a library like: https://github.com/scottyab/secure-preferences

To encrypt the preferences or use a secret key to encrypt and decrypt the data.

You can initialized the user object on the OnCreate of your Application Object this way. Have you app instance be a singleton and you can access this user object object amongst your activities and fragments.

Jian Chen
  • 444
  • 4
  • 10
  • I just want to thank everyone in comments and you, for helping out. Did not knew preferences are somewhat unsecure, ty for the library – MePo Sep 26 '17 at 10:47