2

I've recently been looking at options available to me for persisting data across Activities. I've searched a bit online and only found a few methods that other people seem to use. Some of them are -

  • Intent
  • Singleton class
  • Disk
  • Application

What about using a Service? I haven't found anywhere where the use of Service for data persistence across Activities is mentioned. From what I've experienced with Android Service, I'm pretty sure that it can be used for this purpose. Is there any reason why others, as far as I know, do not use Service for data persistence?

David Heisnam
  • 2,463
  • 1
  • 21
  • 32

2 Answers2

1

Of course you can do that, but it is much more overhead (and you need to write more code, and you need to understand more about Android).

In any case, a Service isn't any more "persistent" than using public static variables (which is pretty much the same as using the Application class or a singleton). If Android needs resources, or your app hasn't been used in a while, or your phone is just having a bad day, Android will kill the OS process hosting your Service, your singleton, your Application class and your public static variables. So if you want to be more "persistent" than that, you'll need to use a database, shared preferences or a file.

David Wasser
  • 93,459
  • 16
  • 209
  • 274
0

use LocalBroadcastManager Example

Itzik Samara
  • 2,278
  • 1
  • 14
  • 18