Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.

What is the Activity

Activity, as one of the four components, can be said to be the most familiar to Android engineers, but also the most intimate contact with the component. It is a user interface component, mainly including display interface information and processing user interaction. Of course, an Activity doesn’t have to be displayed in front of the user. In some cases, an Activity can be invisible.

Four kinds of state

Before you understand the life cycle, you need to know the four states of the Activity:

  • Running state: The Activity is at the top of the stack, visible to the user, and has the lowest recycling priority.
  • Paused: The Activity is not at the top of the stack, but is still visible to the user. This is most often the case when an Activity is overwritten by another transparent Activity or blocked by a popover. Only in the case of severely low memory, the system will consider reclamation.
  • Stopped state: The state when the Activity is not at the top of the stack, but is completely invisible. This Activity is reclaimed higher in priority than paused activities.
  • Destroyed state: The Activty becomes destroyed when it is removed from the stack and is the easiest Activity to recycle.

The life cycle

  • OnCreate: Indicates that the Activity is being created. This method allows you to perform initialization operations, such as setting the layout and loading the data required for the page, but not time-consuming operations.
  • OnStart: Indicates that the Activity is started and changes from the invisible state to the visible state, but cannot interact with the user.
  • OnResume: The Activity is called when it is ready to be used to interact with the user.
  • OnPause: Indicates that the current Activity has lost focus. This is usually invoked when another Activity starts or a pop-up box appears.
  • OnStop: is invoked when the current Activity is no longer visible.
  • OnRestart: called when returning from an Activity to the current Activity.
  • OnDestory: Called when the Activity is killed or when the Finish method is actively called to indicate that the Activity is destroyed.

Boot mode

  • Standard: The default startup mode for an Activity. In this mode, an Activity creates an instance and places it at the top of the stack, allowing multiple instances.
  • SingleTop: Multiple activities can exist in the stack, but the same Activity is not allowed to be superimposed. That is, if the same Activity instance already exists at the top of the stack, the new instance will not be created, but will be reused and the onNewIntent method will be called.
  • SingleTask: Only one instance of an Activity is allowed. If the Activity instance exists in the current stack, it will be moved to the top of the stack when the same Activity is started again and the onNewIntent method will be called. It also removes other Activity instances between Activity launches.
  • SingleInstance: Only one instance exists, and this instance exists in a separate task. Other instances are not allowed.