Saturday, 9 August 2014

6. Android Components Lifecycle

An android application can have several components. and every application runs in its own Linux process. Process gets created whenever any one of the components gets activated. In android, every component has its own lifecycle.

Activity Lifecycle

An activity could be in 3 states: resumed, paused or stopped.

In resumed state, activity is in the foreground, occupying the screen and this is the activity that will respond to any manipulation of the screen by the user.
In paused state, an activity has lost focus but still visible to the user. However, it will not receive any input. For example, dialog box pops up on the screen and hides the activity in the background.
In the stopped state, the activity is completely off screen. We cannot see it but is there somewhere in the background. Activity will still retains all states and member information in stopped state.

Within android, an activity goes through a lifecycle as shown in below fig (activity lifecycle methods):

As activity transition from one state to another, it is notified by the change by calls to these protected methods: onCreate(), onStart(), onRestart(), onResume(), onPause(), onStop(), onDestroy().
  • onCreate() - called when activity is first created.
  • onStart() - called just before when activity becomes visible to user.
  • onRestart() - called after the activity has been stopped, just prior to being started again.
  • onResume() - called just before the activity starts interacting with the user.
  • onPause() - called when system is about to start resuming another activity. This method is used to commit unsaved changes, stop animations etc.
  • onStop() - called when activity is no longer visible to user (being destroyed or covered by another activity).
  • onDestroy() - called before the activity is destroyed.

Service Lifecycle

A service always runs in the background and thus never occupies the screen. However, an activity in the foreground needs to interact with the service, for ex- if we click download button on the screen, the download work has to be offloaded to the service in the background. So activity needs to issue a call to the service for this.

We can use service in two ways:
  • The service can be started and allowed to run until someone stops it or stops itself. Here, it can be started by calling Context.startService() and can be stopped by calling Context.stopService().
  • In another way, client establish a connection to the service object and use that connection to call into the service. It is done by calling Context.bindService() and closed by calling Context.unbindService().

Broadcast Receiver

Broadcast Receiver have just one callback method (onReceive()) in its lifecycle. When android realizes that it need to deliver notification, it issues a call to onReceive() callback method of broadcast receiver. So at this point broadcast receiver becomes active. Once the onReceive() method is completed, the receiver disappears.

No comments:

Post a Comment