Saving activity state

When the system, rather than the user, shuts down an activity to conserve memory, the user may expect to return to the activity and find it in its previous state.

• It can be started and allowed to run until someone stops it or it stops itself. In this mode, it's started by calling Context.startService() and stopped by calling Context.stopService() . It can stop itself by calling

Service.stopSelf() or Service.stopSelfResult() . Only one stopService() call is needed to stop the service, no matter how many times startService() was called.

• It can be operated programmatically using an interface that it defines and exports. Clients establish a connection to the Service object and use that connection to call into the service. The connection is established by calling Context.bindService() , and is closed by calling Context.unbindService() . Multiple clients can bind to the same service. If the service has not already been launched, bindService() can optionally launch it.

The two modes are not entirely separate. You can bind to a service that was started with startService() . For example, a background music service could be started by calling startService() with an Intent object that identifies the music to play. Only later, possibly when

«the user wants to exercise some control over the player or get information about the current song, would an activity establish a connection to the 0 service by calling bindService() . In cases like this, stopService() will not actually stop the service until the last binding is closed.

Like an activity, a service has lifecycle methods that you can implement to monitor changes in its state. But they are fewer than the activity methods — only three — and they are public, not protected:

0 0

Post a comment