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

How to control the application life cycle under the Android platform

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article shows you how to control the application life cycle under the Android platform, the content is concise and easy to understand, can definitely brighten your eyes, through the detailed introduction of this article, I hope you can get something.

We have introduced to many friends the Android-specific virtual machine: the working principle of the Dalvik virtual machine. The process management and memory management of each Android application at runtime are relatively independent, and the Android application can not control its own life cycle. The editor will show you how to control the application life cycle.

I. Preface

Unlike most traditional operating system environments, Android applications have no control over their lifecycle. So the various components of the application (activity,Service... You have to listen carefully to the impact of changes in the state of the application on them at all times to prevent them from being terminated at an inappropriate time. (it may cause data loss.)

In Android, each application has an independent process running on a separate Dalvik (an Android-specific virtual machine). The process management and memory management of each application at run time are relatively independent. Android uses an "intrusive" way to manage system resources, which means that in order to release resources to high-priority programs (usually programs that are interacting directly with users), some processes and their hosts will be mercilessly terminated without any warning.

Second, understand the priority and process status of the application

Some processes are terminated in order to release recycled resources, so which processes will be terminated? Does An end first or B end first? This depends on the priority of the host application. The priority of an application is equal to the priority of components with * priority. If the two programs have the same priority at the moment, which process has been in the low priority for a long time will be terminated. The priority of the process is affected by the inter-process dependency, for example, the Service or Content Provider that An application depends on is provided by B application, then B will have a higher priority. All Android applications are left to run in memory until the system needs to release recycled resources.

Figure: various processes and their corresponding priorities

The Activity Process active process is a component in which its host program interacts more directly with users, and Android will try to ensure its running state by recycling various resources. The number of such processes is small and is usually terminated.

Active processes include the following:

Activity is active, that is, it is in foreground, receiving and responding to user events

Activities, Services, or Broadcast Receivers are executing to handle OnReceive events

Services is executing to handle onStart, onCreate, or onDestroy events.

Visible Process visualization, but not active Activity. They are not in foreground and do not accept responses to user events. This happens when the Activity is partially overwritten (for example, we click a button in A, a Dialog pops up, and A becomes a visual process). Visual processes are also rare, and only in extreme cases will they be terminated in order to ensure that Activity Process is running.

Started Service Process note that it is the service process that is started. The service process needs to run continuously without a visual interface because the service does not interact directly with the user. They are still considered foreground processes and are terminated only when Visible and Activity Process need resources.

Background Process A process attached to the invisible Activity without any started service process becomes a background process. Usually there are a lot of background processes in the Android, and Android uses last-seen-first-killed to end such processes, thus providing resources for the foreground process.

Empty Process in order to improve the performance of the entire system, Android will keep applications that have ended their life cycle in memory. Android retains these caches so that the application can be started quickly. They will be terminated in the usual way.

To sum up, it is very important to organize the application correctly, ensure the stable operation, and prevent the operation from being terminated.

The above content is how to control the application life cycle on the Android platform. Have you learned the knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.

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

Development

Wechat

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

12
Report