Life cycle of Activity in Android application

Today I would like to introduce you to the life cycle of 1 Activity in Android application. Mastering this will help you better understand an Android application and help a lot in the process of developing the application later.

The activities in the system are managed as an activity stack. When a new activity starts it is placed on the top of the stack and becomes a Running Activity (running activity), at the same time the previous activity will be just below it, and will not become visible (see) until the above activity exits the stack. Activity 1 includes 4 main states:

  • If activity is at the top of the screen (or at the top of the stack), it is in the active / running state . For example, when we call, the activity of dialing that number is in active status.
  • If the activity cannot be interacted but is still visible (when it is hidden by another activity, but the user can still see it in the background), the activity is in paused state. When in this state, the activity can be removed by the system when the device lacks memory. For example, when there is a message pops up covering part of the activity, the activity enters a paused state.
  • If the activity is completely obscured by another activity, it is in a stopped state. This activity retains all status and information, but is no longer visible to users and is often removed by the system when memory is missing. For example, when we turn off the screen, when it is active, it stops.
  • If the activity is in a paused or stopped state , the system can remove that activity from memory by asking it to end or delete its process. When that acitivty is displayed again to the user, it will be reset and restored to its previous state.

The following image illustrates the life of an Activity along with its statuses:

Android-Activity-Lifecycle

The status of activity via each method:

  • onCreate () : the activity is initialized
  • onStart () : the activity becomes visible.
  • onResume () : the activity is still visible (but may return from paused)
  • onPause () : 1 activity masked an existing activity section.
  • onStop () : the activity is no longer visible.
  • onDestroy () : the activity is canceled.

There are 3 main loops that you need to know:

  • Entire lifetime : occurs between onCreate () and onDestroy (). 1 activity will install the status in onCreate () and release all resources in onDestroy (). For example, if there is an implicit thread running data from the network, we can create a thread at onCreate () and end the thread at onDestroy ().
  • Visible lifetime : occurs between onStart () and onStop (). During this stage, the user can see the activity on the screen, but it is not on the top of the stack and cannot interact with the user. Between these two methods, users can store the necessary resources displayed on the activity for users. For example, if we need to observe changes that affect the interface, we can set the Broadcast Receiver at onStart () and remove atStop ().
  • Foreground lifetime : occurs between onResume () and onPause (). In this stage, activity is at the top of the stack and can interact with users.

So I introduced through the life of an activity, hope you can understand and master this knowledge. Good luck!

Source: https://developer.android.com/reference/android/app/Activity.html

Share the news now