D3bian0s
D3bian0s

Reputation: 51

Is Singleton the best way to keep data in memory on an Android application?

I am building an Android application that has a class User which is used for keeping the profile information of the user.

When a user is created it is saved on a SharedPreferences file so I can load the last created user then next time I start the app. I also have a Singleton class which holds the object of the newly created user so it can be used by the other classes.

I did this to avoid reading the SharedPreferences file every time I change a Fragment and the user is needed.

My question is:
Is using Singleton a proper way for keeping variables in memory for the entire application?
Was it intended for that or is there another way to do that that keeps the Object Oriented Programming principles?
Would it be better to create variables in Activity instead with accessor methods?

Upvotes: 3

Views: 2773

Answers (3)

Bitwise DEVS
Bitwise DEVS

Reputation: 3449

I know this is a very old post but I don't want others specially new comers to do this mistake. The answer is NO. The reason is because Singletons are tied to your app process which can be killed anytime by Android system when it needs resource and your app is in background or not being used (e.g. coming from lock screen). If you ever wanted to do this then just make sure you re-initialize everything you use in that Singleton before calling it or using it else your app can crash. Another problem from that crash is its report, you will most likely get a wrong crash report pointing to other scenario instead of the actual underlying issue in your code, this is code smell in a nutshell. Instead do use a proper solution such as local database or even SharedPreferences.

TLDR: This is a good article that discuss other issue when using Singleton on Android.

Upvotes: 0

charlag
charlag

Reputation: 1037

I would use the following: Interface CurrentUser. CurrentUser has methods to retrieve and update user. User may be stored in DB, preferences or mixed. I would use Dagger to inject CurrentUser when needed. CurrentUser implementation should itself be a singleton, because Dagger doesn't guarantee qualities of singleton. Bonus points if your CurrentUser has something like RxJava stream or LiveData to keep observers up-to-date.

As an alternative to Singleton you may want to implement Fragment Holder pattern with retained fragments but in your situation Singleton seems better. Just make sure that you use interfaces and injection to not compromise testability and to keep your components separated.

Upvotes: 2

Abdul Waheed
Abdul Waheed

Reputation: 4678

Yes, it is better way to do so. And it is better to keep data(as you mentioned the case) in Singleton object rather access everytime from shared preference. We user singleton for session purpose in app that means as long as app is running variables data will be accessible.You can create variables on Activity level but that would be very bad approach. In short the approach you are using for your purpose is the best way.

Upvotes: 0

Related Questions