Reputation: 498
I have activity that run and do its work, and then run a service (that will not finish when the activity finish.) All is ok. My question: the service uses values that declared in the activity class as static public, does these values reset when the the activity finish and the service continue working and using them? What happens to these values when the activity start again.
Upvotes: 0
Views: 78
Reputation: 5302
According to Android document
How do I pass data between Activities/Services within a single application?
A public static field/method
An alternate way to make data accessible across Activities/Services is to use public static fields and/or methods. You can access these static fields from any other class in your application. To share an object, the activity which creates your object sets a static field to point to this object and any other activity that wants to use this object just accesses this static field.
I think static will remain in the memory as long as the onDestroy() of the activity is not called. Once it is called static members will be removed from memory.
If you need to persist your data then you can use sharedprefrences.
Activity/Service – inherit from ContextWrapper which implements the same API, but proxies all of its method calls to a hidden internal Context instance, also known as its base context. Whenever the framework creates a new Activity or Service instance, it also creates a new ContextImpl instance to do all of the heavy lifting that either component will wrap. Each Activity or Service, and their corresponding base context, are unique per-instance.
Upvotes: 1