2

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.

  • So I was wondering, is a better way available to me?
  • Would a Service be better?
  • If so, should I start and/or bind it?
Marcin Orlowski
  • 72,056
  • 11
  • 123
  • 141
shkschneider
  • 17,833
  • 13
  • 59
  • 112

1 Answers1

3

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.

NikkyD
  • 2,209
  • 1
  • 16
  • 31