Home > Android > Android Activity Lifecycle Concepts

Android Activity Lifecycle Concepts

Activity is the presenter of a single screen in the application. It is the only one who has certain abilities, like displaying Views, menus, alerts and notifications. It also has the ability to open a new Activity, which means opening a new screen.
Activity is a class which derives from android.app.Activity.
An application needs to have at least one Activity.
All Activities must be declared in the manifest file.

android activity lifecycle

The Activity class defines the following call backs i.e. events. You don’t need to implement all the callbacks methods. However, it’s important that you understand each one and implement those that ensure your app behaves the way users expect.

//Callback  Description
onCreate()  This is the first callback and called when
            the activity is first created.
onStart()   This callback is called when the
            activity becomes visible to the user.
onResume()  This is called when the user starts interacting
            with the application.
onPause()   The paused activity does not receive user
            input and cannot execute any code and called
            when the current activity is being paused and
            the previous activity is being resumed.
onStop()    This callback is called when the activity is
            no longer visible.
onDestroy() This callback is called before the activity
            is destroyed by the system.
onRestart() This callback is called when the activity
            restarts after stopping it.

Activity’s Example

public class MainActivity extends Activity {
   String msg = "Android : ";
   /** Called when the activity is first created. */
   public void onCreate(Bundle savedInstanceState) {
      Log.d(msg, "The onCreate() event");
   /** Called when the activity is about to become visible. */
   protected void onStart() {
      Log.d(msg, "The onStart() event");
   /** Called when the activity has become visible. */
   protected void onResume() {
      Log.d(msg, "The onResume() event");
   /** Called when another activity is taking focus. */
   protected void onPause() {
      Log.d(msg, "The onPause() event");
   /** Called when the activity is no longer visible. */
   protected void onStop() {
      Log.d(msg, "The onStop() event");
   /** Called just before the activity is destroyed. */
   public void onDestroy() {
      Log.d(msg, "The onDestroy() event");