Make the lifecycle of a service independent of activity

My service is usually triggered by a BroadcastReceiver for RECEIVE_BOOT_COMPLETED and implemented with a START_STICKY sign like this:

@Override
public int onStartCommand(Intent intent, int flags, int startId) {   
    return Service.START_STICKY;
}

      

So, in many cases, when the user launches the application, the service will already be running in the background. Then my activity binds to it to read information from the service.

However, when the user then destroys the activity (ending the application), my service that was already started is also destroyed and then restarted via start_sticky. This leads to some information that I store in the service to be lost.

Is there a way to keep my service instance and just keep my actions coming and going, binding to it as needed?

+3


source to share


1 answer


There is no way to ensure that the service (or any component of the application) will not get killed. If you have data that needs to be saved, you can use any of these methods.

http://developer.android.com/guide/topics/data/data-storage.html

SharedPerferences is pretty easy to use.



Here is a section in the developer document (and link) that explains how applications are started and stopped by the system.

By default, each application runs in its own Linux process. Android starts a process when any of the components of an application needs to be executed, then ends the process when it is no longer needed or when the system needs to restore memory for other applications.

http://developer.android.com/guide/components/fundamentals.html

+2


source







All Articles