Tuesday 3 February 2015

HB Blog 56: Android Lifecycles (Activity/Fragment/Service).

Unlike other programming paradigms in which apps are launched with a main() method, the Android system initiates code in an Activity instance by invoking specific callback methods that correspond to specific stages of its life cycle. There is a sequence of callback methods that start up an activity and a sequence of callback methods that tear down an activity.
The activity life cycle consists methods such as onCreate(), onStart(), onResume(), onRestart(), onPause(), onStop() and onDestroy().

onCreate() - Called when the activity is starting. This is where most initialization should go: calling setContentView(int) to inflate the activity's UI, using findViewById(int) to programmatically interact with widgets in the UI, calling managedQuery(android.net.Uri, String[], String, String[], String) to retrieve cursors for data being displayed, etc.
onStart() - Called after onCreate(Bundle) — or after onRestart() when the activity had been stopped, but is now again being displayed to the user. It will be followed by onResume().
onResume() - Called after onRestoreInstanceState(Bundle), onRestart(), or onPause(), for your activity to start interacting with the user. This is a good place to begin animations, open exclusive-access devices (such as the camera), etc.
onRestart() - Called after onStop() when the current activity is being re-displayed to the user (the user has navigated back to it). It will be followed by onStart() and then onResume().
onPause() - Called as part of the activity lifecycle when an activity is going into the background, but has not (yet) been killed. The counterpart to onResume().
onStop() - Called when you are no longer visible to the user. You will next receive either onRestart(), onDestroy(), or nothing, depending on later user activity.
onDestroy() - Perform any final cleanup before an activity is destroyed. This can happen either because the activity is finishing (someone called finish() on it, or because the system is temporarily destroying this instance of the activity to save space. You can distinguish between these two scenarios with the isFinishing() method.

A Fragment represents a behavior or a portion of user interface in an Activity. You can combine multiple fragments in a single activity to build a multi-pane UI and reuse a fragment in multiple activities. You can think of a fragment as a modular section of an activity, which has its own lifecycle, receives its own input events, and which you can add or remove while the activity is running (sort of like a "sub activity" that you can reuse in different activities).
The fragment life cycle consists methods such as onAttach(), onCreate(), onCreateView(), onActivityCreated(), onStart(), onResume(), onPause(), onStop(),
onDestroyView(), onDestroy(), onDetach().

onAttach() - It is called once the fragment is associated with its activity.
onCreate() - It is called to do initial creation of the fragment.
onCreateView() - It creates and returns the view hierarchy associated with the fragment.
onActivityCreated() - It tells the fragment that its activity has completed its own Activity.onCreate()
onStart() - It makes the fragment visible to the user (based on its containing activity being started).
onResume() - It makes the fragment interacting with the user (based on its containing activity being resumed).
onPause() -  It is called when fragment is no longer interacting with the user either because its activity is being paused or a fragment operation is modifying it in the activity.
onStop() - It is called when  fragment is no longer visible to the user either because its activity is being stopped or a fragment operation is modifying it in the activity.
onDestroyView() - It allows the fragment to clean up resources associated with its View.
onDestroy() - It is   called to do final cleanup of the fragment's state.
onDetach() - It is   called immediately prior to the fragment no longer being associated with its activity.

A Service is an application component that can perform long-running operations in the background and does not provide a user interface.Additionally, a component can bind to a service to interact with it and even perform interprocess communication (IPC). Like 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. Unlike the activity lifecycle callback methods, you are not required to call the superclass implementation of these callback methods.The services life cycle consists methods such as,

Unbounded Services: onCreate(), onStartCommand(), onDestroy().
onCreate() - The system calls this method when the service is first created, to perform one-time setup procedures.
onStartCommand() - The system calls this method when another component, such as an activity, requests that the service be started, by calling startService().
onDestroy() - The system calls this method when the service is no longer used and is being destroyed.

Bounded Services: onCreate(), onBind(), onUnbind(), onDestroy().
onCreate() - It is called by the system when the service is first created. Do not call this method directly.
onBind() - The system calls this method when another component wants to bind with the service (such as to perform RPC), by calling bindService().
onUnbind() - It is called when all clients have disconnected from a particular interface published by the service.
onDestroy() - The system calls this method when the service is no longer used and is being destroyed. 

No comments:

Post a Comment