The basic concept of Activity and the life cycle of Activity

First, the basic concept of Activity

  Activity is one of the four major components of Android. It is a component that can contain user interface and is mainly used to interact with users, such as making calls, taking pictures, sending emails, or displaying a map! Activity is used to display the user interface, and the user completes related operations through Activity interaction. An App allows multiple Activities.

Second, the life cycle of Activity

  Activity life cycle is something that every Android developer must master. When we deeply understand the life cycle of activities, we can write more coherent and smooth programs, so that our programs have a better user experience.

 2.1, Activity life cycle diagram

  Let’s take a look at the picture first, the picture looks clear at a glance. Image source (http://www.runoob.com/w3cnote/android-tutorial-activity.html)

2.2. Four states of Activity

  Each Activity may have up to four states during its life cycle.

1. Running status

  When an Activity is at the top of the back stack (the concept of the back stack will be introduced later), then the Activity is in the running state, and the system will display the Activity at the top of the stack to the user.

2. Pause state

  When an Activity is no longer at the top of the stack, but still visible, the Activity enters the paused state. Beginners may have this question, since the Activity is no longer at the top of the stack, how can it still be visible? This is because not every Activity will occupy the entire screen. For example, the Activity in the form of a dialog box will only occupy the middle of the screen. part of the area.

3. Stop state

   When an Activity is no longer at the top of the stack and is completely invisible, it enters the stopped state.

4. Destruction state

  When an Activity is removed from the back stack, it becomes the destroyed state.

2.3, Android return stack

  Android uses tasks to manage activities. A task is a set of activities stored in a stack. This stack is called a return stack. A stack is a first-in, last-out data structure . Let's take another common data structure: Queue, a queue is a first-in, first-out data structure.

Whenever a new Activity is started, it is placed on the back stack and is at the top of the stack. Whenever we press the Back key or call the activity's finish() method to destroy an activity, the Activity at the top of the stack will pop out of the stack, and the previously pushed Activity will be at the top of the stack again. The system always displays the Activity at the top of the stack to the user.

 2.4, Activity lifetime

  There are 7 callback methods defined in the Activity class, covering every link of the Activity life cycle. Let's introduce these 7 methods one by one.

1.onCreate()

  This method is available in every Activity class. When we create a new Activity class, we must override the onCreate method of the parent class. The onCreate method will be called when the Activity is first created . We should complete the initialization of the Activity in this method, such as loading the layout, initializing the layout controls, binding button events, and so on.

2.onStart()

  This method is called when the Activity changes from invisible to visible.

3.onResume ()

  This method is called when the Activity is ready for user interaction. At this time, the Activity must be at the top of the return stack and in the running state.

4.onPause()

  This method is called when the system is ready to start or resume another Activity.

5.onStop ()

  This method is called when the Activity is completely invisible. The main difference between it and the onPause() method is that if the new Activity started is a dialog-like activity, the onPause() method will be executed, but the onStop() method will not be executed.

6.onDestory ()

  This method is called before the Activity is destroyed, after which the state of the Activity will change to the destroyed state.

7.onRestart()

  This method is called before the Activity changes from the stopped state to the running state, that is, the Activity is restarted.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324654349&siteId=291194637