Android Lifecycle of a Service


The services lifecycle has the following callbacks

  • onCreate() :

Executed when the service is first created in order to set up the initial configurations you might need. This method is executed only if the service is not already running.

  • onStartCommand() :

Executed every time startService() is invoked by another component, like an Activity or a BroadcastReceiver. When you use this method, the Service will run until you call stopSelf() or stopService(). Note that regardless of how many times you call onStartCommand(), the methods stopSelf() and stopService() must be invoked only once in order to stop the service.

  • onBind() :

Executed when a component calls bindService() and returns an instance of IBInder, providing a communication channel to the Service. A call to bindService() will keep the service running as long as there are clients bound to it.

  • onDestroy() :

Executed when the service is no longer in use and allows for disposal of resources that have been allocated.

It is important to note that during the lifecycle of a service other callbacks might be invoked such as onConfigurationChanged() and onLowMemory()

enter image description here