Activity life cycle

Google Developers
Android Developers

1. Life cycle

1、onCreate()

This callback method must be implemented. This method is fired when the system creates an Activity.
Inside this callback, the basic components of the activity need to be initialized: for example, the view needs to be created and the required data bound. More importantly, must be called setContentView()to define the layout of the activity's user interface.

When onCreate() finishes, the next callback is always onStart().

2、onStart()

onCreate()After execution is complete, the activity enters the Startedstate and the activity becomes visible to the user.

This callback method contains the final preparation of the activity (coming to the foreground and becoming interactive).

3、onResume()

The system calls this callback only before the activity starts interacting with the user. At this point, the activity is at the top of the activity stack and captures all user input. Most of the core functionality of the application is implemented in the onResume()method .

The onPause() callback always follows onResume().

4、onPause()

The system calls this callback when the activity loses focus and enters a paused state. For example: this state is triggered when the user taps the back announcement.

When the system calls onPause(), it technically means that your activity is still partially visible, but it is usually the knowledge that the user leaves the activity, and the activity will soon go to a stopped or resumed state.

If the user desires to update the UI, the activity in the paused state can continue to update the UI. Examples of such activities include displaying a navigation map screen or playing a media player. Even if such an activity loses focus, users want their UI to continue to update.

Should not be used onPause()to save application or user data, make network calls, or perform database transactions. For information on saving data, see Saving and Restoring Active State.

Once onPause() finishes executing, the next callback is either onStop() or onResume(), depending on what happens after the activity enters the Paused state.

5、onStop()

System call when the activity is not visible to the user onStop(). This can be because an activity is destroyed, a new activity starts, or an existing activity enters a resumed state and overwrites the stopped activity. In all of these cases, the activity to stop pretending is not visible at all.

The next callback to the system call is onRestart()if the activity returns to interacting with the user; or onDestroy(), if this activity terminates completely.

6、onRestart()

The system calls this callback when an activity in a stopped state is about to restart.
onRestart()Resume the active state from when it was stopped.

This callback is always followed by onStart().

7、onDestroy()

The system calls this callback before the activity is destroyed.

This callback is the last callback received by the activity. onDestroy()Usually implemented to ensure that all activity's resources are released when the activity or the process containing it is destroyed.

2. Understand the life cycle

Enter image description

Guess you like

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