Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the life cycle of Android?

2025-04-08 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article introduces the relevant knowledge of "what the Android life cycle is like". Many people will encounter such a dilemma in the operation of actual cases, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

I. Life cycle under normal circumstances

1. Overview of life cycle

● onCreate: represents the creation of an Activity. (the first phase of the life cycle) function: complete the initialization work, such as: loading page layout resources, initializing data.

● onStart: indicates that the Activity is being started and is about to begin. Function: the page is visible, but cannot interact with the user.

● onResume: indicates that the Activity appears in the foreground. Function: compared with onStart, onStart is in the background and OnResume is displayed in the foreground.

● onPause: indicates that the page (Activity) is stopping. Function: the page is in the background, normally, onStop is followed by execution. At this point, you will do some work that is not too time-consuming to store data and stop animation. The onResume of the new page (Activity) will not be executed until the onPause executes.

● onStop: indicates that the Activity is about to stop. Function: the page is invisible, doing slightly lightweight, less time-consuming recycling work.

● onDestroy: indicates that the Activity is about to be destroyed. (the last phase of the life cycle) function: recycling work and release of resources.

● onRestart: indicates that the Activity is restarted. Function: this method is called when the page transitions from invisible to visible. Such as: Home key to switch the page (open a new Activity), and then return to the page process.

2. Find out two problems

Question 1: onStart is similar to onResume, onPause and onStop in terms of description. What is the real difference for us?

A: onStart and onStop are callback from the perspective of whether Activity is visible, while onResume and onPause are callback from whether they are located in the foreground.

Question 2: assume that the current Activity is A. If the user opens a new Activity B, which executes first, the onResume of B or the onPause of A.

Answer: according to the basic operation mechanism of Android, you cannot perform heavyweight operations in onPause, because you must complete the execution of onPause before the new Activity can onResume. Neither onPause nor onResume can perform time-consuming operations, especially onPause, which means that we should do operations in onStop. This allows the new Activity to be displayed and switched to the foreground.

2. Life cycle under abnormal circumstances

Case 1: resource-related system configuration changes cause Activity to be killed and recreated

For example, if the current Activity is vertically displayed, if the screen is suddenly rotated, the Activity will be destroyed and recreated by default due to a change in the system configuration.

1. Process analysis

① Activity will be destroyed, where onPause, onStop, and onDestory will all be called, and because Activity is terminated under abnormal circumstances

The ② system calls onSavaInstanceState to save the current Activity state. This method is called before onStop, it has no established timing relationship with onPause, it may be called before or after onPause. This will only happen if the Activity is abnormally terminated.

③ when the Activity is recreated, the system calls onRestoreInstanceState and passes the Bundle object saved by the onSaveInstanceState method when the Activity is destroyed as a parameter to both the onRestoreInstanceState and onCreate methods. So we can determine whether the onRestoreInstanceState and onCreate methods are rebuilt.

If ④ is rebuilt, we will take out the previously saved data and restore it. In terms of timing, the call to onRestoreInstanceState comes after onStart.

2. Note: when we terminate the Activity and recreate the data state, there are two ways to receive it. The receiving location can choose the difference between onRestoreInstanceState (officially recommended) or onCreate:

Once ① onRestoreInstanceState is called, its parameter Bundle saveInstanceState must have a value, so we don't have to determine whether it is empty or not.

If ② onCreate starts normally, its parameter Bundle saveInstanceState is null, so additional judgment must be made.

The ③ system will only call onSaveInstanceState and onRestoreInstanceState to store and recover data when the Activity terminates abnormally. Otherwise, this process will not begin.

Case 2: insufficient resources and memory causes low-priority Activity to be killed

1. Priority of Activity

First, describe the priority of Activity. The priority of Activity can be divided into the following three categories:

① foreground Activity-the Activity that is interacting with the user, with the highest priority.

② visible but not foreground Activity-for example, a dialog box pops up in Activity, causing the Activity to be visible but in the background and unable to interact directly with the user.

③ background Activity-the Activity that has been suspended, such as the execution of onStop, has the lowest priority.

2. Process analysis when resources are out of memory:

When the system resources are insufficient, the system will kill the process of the target's Activity according to the above priority, and then store and recover the data through onSaveInstanceState and onRestoreInstanceState. If some background processes run independently from the four components, the process will be killed quickly. We often put background work in Service to keep the process a certain priority.

3. Question: when the system changes, we don't want the Activity to change, for example, when we rotate the screen, we don't want to recreate the new Activity, what do we do?

Answer: use the android:configChanges= "orientation" attribute.

If we do not specify an option in the configChanges property, the Activity will be recreated when the system configuration changes. There are three attributes we often use:

① locale: the local location of the device has changed, which generally refers to the switching of the system language.

② keyboardHidden: keyboard accessibility has changed, such as when the user calls the keyboard.

③ orientation: the orientation of the screen has changed, and this is the most commonly used, such as a rotating mobile phone screen, which is usually used in conjunction with the screenSize attribute value.

In this way, the Activity will not be created, and the onSaveInstanceState and onRestoreInstanceState methods will not be called. Instead, the system calls the onConfigurationChanged method, and we can do some special processing at this time.

That's all for "what the Android Life cycle is like". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report