Activity Life Cycle

Responsive image
0 Comments         3010 Views

What is Android Activity Life Cycle?

Activity Lifecycle: Activity is one of the building blocks of Android OS. In simple words Activity is a screen that user interact with. Every Activity in android has lifecycle like created, started, resumed, paused, stopped or destroyed. These different states are known as Activity Lifecycle.

  1. The activity gets launched.

    The activity object is created and its constructor is run.

  2. The onCreate() method runs immediately after the activity is launched.

    The onCreate() method is where any initialization code should go, as this method always gets called after the activity has launched and before it starts running.

    Once the activity starts three steps will be automatically start.

    1. On create
    2. On start
    3. On resume

  3. An activity is running when it’s visible in the foreground and the user can interact with it.

    This is where an activity spends most of its life.

  4. The onDestroy() method runs immediately before the activity is destroyed.

    The onDestroy() method enables you to perform any final clean up such as freeing up resources.

  5. After the onDestroy() method has run, the activity is destroyed.

    The activity ceases to exist.



A fragment must always be hosted in an activity and a fragment's lifecycle is directly affected by the host activity's lifecycle. For example, when an activity resumes so will all of its fragments and when the activity pauses so will all of the fragments.

What is the lifecycle of foreground activity in Android?

An activity is running when it’s in the foreground of the screen, has the focus, and the user can interact with it. The activity spends most of its life in this state. An activity starts running after it has been launched, and at the end of its life, the activity is destroyed.