Android Service Lifecycle: A Complete Guide for Students (2026)

 What is a Service in Android?

A Service is an application component that can perform long-running operations in the background. A service does not provide a user interface.  

Services do their work even user is not on the application. So services are used for such task which requires continuous processing. 

Key Point: A Service is NOT a separate process or thread. You must manually create a background thread (e.g., using ThreadAsyncTaskCoroutines, or ExecutorService) inside a Service if you want truly background execution.

 

Started Service (Unbound)

Started Service is a service that is launched by an Activity by calling the startService(Intent) method.

Once service started, it runs indefinitely in the background — it is completely independent of the component that started it.

Bound Service

Bound Service is a service that allows application components (like Activities, Fragments, or even other Services) to interact with it.

Foreground Service

Foreground Service is a type of service that performs operations that are visible to the user.

It must display a persistent notification in the status bar while it is running. This notification is the proof to the user that something important is happening in the background.

Note: A service can be both started AND bound at the same time. In that case, the service runs until it is explicitly stopped AND all clients are unbound.

Service Lifecylce

The lifecycle of a service is simpler than the Activity lifecycle. However, it’s even more important that you pay close attention to how your service is created and destroyed. Because a service has no UI, services can continue to run in the background with no way for the user to know, even if the user switches to another app. This situation can potentially consume resources and drain the device battery.

 

Similar to an Activity, a service has lifecycle callback methods that you can implement to monitor changes in the service’s state and perform work at the appropriate times. 

Started Service Lifecycle

When you call startService(Intent), Android creates the service (if not already running) and calls the lifecycle methods in a specific order. Here is the complete lifecycle:

Bound Service Lifecycle

A Bound Service allows other components (like an Activity) to bind to it, send requests, receive responses, and even perform inter-process communication (IPC). The service is active only while at least one component is bound to it.

Key Lifecycle Methods Explained

onCreate()

Called when the service is first created. Use this to set up one-time initialization — like creating a thread or setting up an IPC mechanism. This is called only once during the lifetime of the service.

onStartCommand(Intent, int, int)

Called every time a client starts the service using startService(). This is where your actual work begins. The three parameters are: the Intent passed, a flag (e.g., START_FLAG_REDELIVERY), and the unique start ID.

onBind(Intent)

Called when a component calls bindService(). Must return an IBinder object that clients use to communicate with the service. If binding is not supported, return null.

onUnbind(Intent)

Called when all clients have disconnected. Return true if you want onRebind() to be called when new clients connect in the future.

onRebind(Intent)

Called if onUnbind() returned true and a new client binds to the service.

onDestroy()

Called when the service is no longer used and is being destroyed. Always clean up resources here — stop threads, unregister receivers, close connections.

Code Examples

Creating a Started Service (Java)

public class MyService extends Service {

    @Override
    public void onCreate() {
        super.onCreate();
        // One-time initialization
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        // Do background work here
        new Thread(new Runnable() {
            @Override
            public void run() {
                // Long running task (e.g., download file)
                stopSelf(); // stop when done
            }
        }).start();
        return START_STICKY;
    }

    @Override
    public IBinder onBind(Intent intent) {
        return null; // not a bound service
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        // Cleanup resources
    }
}

Registering Service in AndroidManifest.xml

<service
    android:name=".MyService"
    android:enabled="true"
    android:exported="false" />

Starting and Stopping a Service

// Start service
Intent intent = new Intent(this, MyService.class);
startService(intent);

// Stop service from Activity
stopService(intent);

Common Exam & Interview Questions

Q1: What is the difference between a Started Service and a Bound Service?A Started Service runs independently and does not return results to the caller. A Bound Service provides a two-way communication interface and is tied to the lifecycle of the binding component.
Q2: On which thread does a Service run by default?On themain (UI) thread. You must create a background thread manually for any long-running work.
Q3: What does START_STICKY return value mean?It tells Android to recreate the service after it is killed, but NOT to re-deliver the original Intent.
Q4: When is onDestroy() NOT guaranteed to be called?When the Android system forcibly kills the process due to low memory, onDestroy() may not be called.
Q5: What must you declare in AndroidManifest.xml to use a Service?You must declare it with the <service> tag. Without this declaration, the service will not be found and will throw an exception.
Q6: What is a Foreground Service and when should you use it?A Foreground Service performs tasks that are noticeable to the user (e.g., playing audio, GPS navigation). It requires a persistent notification and has a higher system priority, making it less likely to be killed.
Q7: What is the role of IBinder in a Bound Service?IBinder is the interface that allows the client to call methods on the service. The service returns an IBinder in onBind() and the client uses it to interact with the service directly.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top