Issue
My service is usually started by a BroadcastReceiver for RECEIVE_BOOT_COMPLETED, and is implemented with the START_STICKY flag as follows:
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
return Service.START_STICKY;
}
So In many cases, when the user launches the app, the service would already be running in the background. My activity then binds to it to read information from the service.
However, when the user then destoys the activity (by terminating the app), my service that was already running is now destroyed as well, and it is then restarted via start_sticky. This causes some information that I keep in the service to be lost.
Is there a way to preserve my service instance, and just have my activities come and go, binding to it as needed?
Solution
There is no way to make sure a service (or any component of an app) will not be killed. If you have data that needs to be persisted you can use any of these techniques.
http://developer.android.com/guide/topics/data/data-storage.html
SharedPerferences is pretty easy to use.
Here is the section in the developer doc (and the link) that explains how apps are started and stopped by the system.
By default, every app runs in its own Linux process. Android starts the process when any of the app's components need to be executed, then shuts down the process when it's no longer needed or when the system must recover memory for other apps.
http://developer.android.com/guide/components/fundamentals.html
Answered By - nPn
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.