Home Java javaTutorial Android Development Series 2: The Life Cycle of Window Activity

Android Development Series 2: The Life Cycle of Window Activity

Jan 07, 2017 pm 03:49 PM

In the last article, I introduced to you the use of buttons to display time in the android development series. Interested friends can click to read the details.

In the process of Activity from creation to destruction, 7 life cycle methods need to be called at different stages. These 7 life cycle methods are defined as follows:

protected void onCreate(Bundle savedInstanceState)
protected void onStart()
protected void onResume()
protected void onPause()
protected void onStop()
protected void onRestart()
protected void onDestroy()
Copy after login

The above 7 life cycle methods are called in a certain order in 4 stages.

1, start Activity: At this stage, 3 life cycle methods are executed in sequence, namely onCreate, onStart, onResume

2, Activity loses focus: If you enter another Activity or application while the Activity gains focus, the current Activity will lose focus. At this stage, the onPause and onStop methods will be executed in sequence

3. Activity regains focus: At this time, the onRestart, onStart, and onResume methods will be executed in sequence

4. Close Activity: When the Activity is closed, the system will execute 3 life cycle methods in sequence, namely onPause, onStop, onDestory

If the state does not change during the execution of the life cycle methods in these four stages, the system will execute the life cycle methods in the four stages in sequence in the order above. If the state changes during the execution status, the system will call the life cycle method according to a more complex method

If the Activity regains the focus during the execution of the onStop method, and then loses the focus, the system will not execute the onDestory method, but as follows Sequential execution of life cycle methods

onStop->onRestart->onStart->onResume->onPause->onStop

The following figure describes the activity from creation to destruction and intermediate states The process of calling life cycle methods after changes

It is not difficult to see from the Activity life cycle calling method diagram shown above that the entire Activity life cycle contains two layers of loops. The first layer of loops is onPause- >onResume->onPause, the second layer loop onStop->onRestart->onStart->onResume->onPause->onStop. We can regard these two levels of cycles as sub-life cycles in the entire Activity life cycle.

The first-level loop is called the focus life cycle, and the second-level loop is called the visual life cycle. That is to say, the first-level loop cycles through the process of gaining and losing Activity focus. During the process, Activity is always visible. The second layer of loop is the loop in the process of Activity being visible and invisible. In this process, Activity focus is gained and lost. In other words, Activity will be displayed first, and then it will be displayed. Gain focus, then lose focus, and finally the current activity becomes invisible due to other activities popping up.

Therefore, Activity has the following three life cycles:

1. Overall life cycle: onCreate->....->onDestroy

2. Visual Life cycle: onStart->....->onStop

3. Focus life cycle: onResume->....->onPause

The following codes are in Activity Output log information in the 7 life cycle methods in the class

package com.neil.ad02;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d("onCreate","onCreate Method is executed");
setContentView(R.layout.activity_main);
}
@Override
protected void onDestroy()
{
super.onDestroy();
Log.d("onDestroy","onDestroy Method is executed");
}
@Override
protected void onPause()
{
super.onPause();
Log.d("onPause","onPause Method is executed");
}
@Override
protected void onRestart()
{
super.onRestart();
Log.d("onRestart","onRestart Method is executed");
}
@Override
protected void onResume()
{
super.onResume();
Log.d("onResume","onResume Method is executed");
}
@Override
protected void onStart()
{
super.onStart();
Log.d("onStart","onStart Method is executed");
}
@Override
protected void onStop()
{
super.onStop();
Log.d("onStop","onStop Method is executed");
}
}
Copy after login

Open Android Device Monitor to observe

Android Development Series 2: The Life Cycle of Window Activity

The red box in the picture is to regain focus. You can try the others one by one.

The above content is the life cycle of window activity in the second part of the Android learning series introduced by the editor. I hope it will be helpful to everyone!

For more articles related to the life cycle of window Activity in Android Development Series 2, please pay attention to the PHP Chinese website!


Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to elegantly obtain entity class variable names to build database query conditions? How to elegantly obtain entity class variable names to build database query conditions? Apr 19, 2025 pm 11:42 PM

When using MyBatis-Plus or other ORM frameworks for database operations, it is often necessary to construct query conditions based on the attribute name of the entity class. If you manually every time...

How to simplify field mapping issues in system docking using MapStruct? How to simplify field mapping issues in system docking using MapStruct? Apr 19, 2025 pm 06:21 PM

Field mapping processing in system docking often encounters a difficult problem when performing system docking: how to effectively map the interface fields of system A...

How does IntelliJ IDEA identify the port number of a Spring Boot project without outputting a log? How does IntelliJ IDEA identify the port number of a Spring Boot project without outputting a log? Apr 19, 2025 pm 11:45 PM

Start Spring using IntelliJIDEAUltimate version...

Is the company's security software causing the application to fail to run? How to troubleshoot and solve it? Is the company's security software causing the application to fail to run? How to troubleshoot and solve it? Apr 19, 2025 pm 04:51 PM

Troubleshooting and solutions to the company's security software that causes some applications to not function properly. Many companies will deploy security software in order to ensure internal network security. ...

What is the difference between memory leaks in Java programs on ARM and x86 architecture CPUs? What is the difference between memory leaks in Java programs on ARM and x86 architecture CPUs? Apr 19, 2025 pm 11:18 PM

Analysis of memory leak phenomenon of Java programs on different architecture CPUs. This article will discuss a case where a Java program exhibits different memory behaviors on ARM and x86 architecture CPUs...

How to safely convert Java objects to arrays? How to safely convert Java objects to arrays? Apr 19, 2025 pm 11:33 PM

Conversion of Java Objects and Arrays: In-depth discussion of the risks and correct methods of cast type conversion Many Java beginners will encounter the conversion of an object into an array...

How to convert names to numbers to implement sorting within groups? How to convert names to numbers to implement sorting within groups? Apr 19, 2025 pm 01:57 PM

How to convert names to numbers to implement sorting within groups? When sorting users in groups, it is often necessary to convert the user's name into numbers so that it can be different...

How do I convert names to numbers to implement sorting and maintain consistency in groups? How do I convert names to numbers to implement sorting and maintain consistency in groups? Apr 19, 2025 pm 11:30 PM

Solutions to convert names to numbers to implement sorting In many application scenarios, users may need to sort in groups, especially in one...

See all articles