David Heisnam
David Heisnam

Reputation: 2491

Using Service to share/persist data across Activities

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 -

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?

Upvotes: 2

Views: 163

Answers (2)

David Wasser
David Wasser

Reputation: 95568

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.

Upvotes: 1

Itzik Samara
Itzik Samara

Reputation: 2288

use LocalBroadcastManager Example

Upvotes: 0

Related Questions