shkschneider
shkschneider

Reputation: 18243

Service or not for persistent data

I have an application that keeps some complex data in memory between activities.

As for now I use a Singleton class that use SharedPreferences in getters and setters.

What I want: As long as my application is live and showing in the recent apps, I want a class to never get released or find a way to achieve this another way without consequences.

Upvotes: 2

Views: 161

Answers (1)

NikkyD
NikkyD

Reputation: 2284

If you go with a service, you wouldn't bind it as unbinding could cause it to stop.

You could create a static object and create it in a custom Application class. So for as long as your application is alive the object is held by a strong reference.

Or a combination, use a singleton class but let the application class store the reference to prevent garbagecollection(GC)

after chat:

a service running in its own process should be the most persistent thing you could build. However you need to communicate with the service via AIDL, a cross-process bridge, which draws performance if the communication is high-speed.

Upvotes: 3

Related Questions