I am using MVC structure and I use retrofit to make http requests. I use User model for mapping users data to it by GSON converter in retrofit. I need the logged in user to have static data across all activities i.e (ID, name, etc..). and also some methods such as updateToken, logout, etc.. and I can't make it a Singleton because I want the same class for mapping Json results as I mentioned. So what is the best practise to achieve that? and is it good to used static members to store user data across all activities so retriving and updating user data through them?
Asked
Active
Viewed 108 times
-1
-
then there is an option that you can use EventBus Library – Shubham Jain Oct 03 '17 at 12:12
-
I believe this can be achieved by static methods in the user model without needing to external library, the problem is the same class is being used as a data model for retrofit and for the logged in user. should I separate? – Mody Sharf Oct 03 '17 at 12:14
-
You do not want to consider using SharedPreferences? – Samuel Agbede Oct 03 '17 at 12:16
-
You can keep static data at application instance, but you will loose once you swipe/ kill the app. Better to write the data into persistant storage, 1. Store in database(Remove that when logout) 2. Store into shared preferences and reterive it. 3. Create a file and store the data into it, once logout delete/ flush the file. Event bus one means to send user defined object among activities just like sending with intent, but when you swipe/ kill your app, data will be lost. If required then I can tell the solution for mentioned 3 pointers. – Priyavrat Oct 03 '17 at 12:17
-
I actually use sharedPreference already to store logged user token and data and I retrieve and store them in the static members in the startup activity to easily access them and also I need to do some functions like update token and logout so I need a methods not just properties – Mody Sharf Oct 03 '17 at 12:19
-
u can do one thing place your user model in another package and make it public so that other class can access it and u can reuse your user model as u are going to call the API then new Response will be fetched and your model data is going to renew.it will not contain the same result – Shubham Jain Oct 03 '17 at 12:19
-
I am thinking of using two models. one called User to store users data from retrofit and another one called loggedUser to only handle the logged user – Mody Sharf Oct 03 '17 at 12:24
2 Answers
0
I think you should create a new class to hold these variables and make them static.
-
Is it better to make them static or create the class Singleton? I mean if it is singleton will it keeps the data across all activities? – Mody Sharf Oct 03 '17 at 12:35
-
Yes, if you make a Singleton, it will keep the data across all activities and it's often better than static classes :) – Oct 03 '17 at 12:37
0
You can use shared preferences to store the model class data converted to JSON format. This JSON is converted to a string and stored in shared preferences. Whenever you need to fetch the data call the string from shared preferences and parse it into model class again using GSON.
Check this link https://stackoverflow.com/a/18463758/1791551

Seenu69
- 1,041
- 2
- 15
- 33
-
It is a headache process, I think loading the sharedPreference once into a singleton would be better in terms of implementation and performance also – Mody Sharf Oct 04 '17 at 00:10