Fourth three cornerstones of Android development-activity, service and handler (1)

Source: Internet
Author: User

Fourth three cornerstones of Android development-activity, service and handler

To learn Android development, you have to learn the two components of activity and service first. Activity is an interface-aware program that almost carries all of the user's actions on the application. and service is no interface program, it is called service, also called background program. Mastering them is an essential part of our learning about Android development. Handler is the most commonly used messaging mechanism in Android development, and almost all applications use handler to deliver messages. It can be said that to learn Android application development, you have to learn activity, service development, learning how to use the handler mechanism. This chapter provides details on how to use them for Android development.

4.1 Interface of the Application-activity window

Activity is the most basic and most common component in Android. It is the core of Android, a component that is used to interact with users and Android internal features, and all activity in the application needs to be registered in the Androidmanifest.xml file. So how is activity a component, how it interacts with the display, how an activity instance is managed and run, and what is the activity life cycle like? These are the things we need to know.

4.1.1Activity life cycle

In an Android application, an activity is usually a separate screen, and the life cycle of an activity is the life cycle of the process in which it resides. In Android, Activity has four basic statuses:

1) Active (active/running) When a new activity is launched into the stack, it is at the top of the screen, at the topmost edge of the stack, where it is visible and can interact with the user's activation state;

2) Pause (Paused) when activity is overwritten by another transparent or Dialog-style activity. At this point it remains connected to the window manager, the system continues to maintain its internal state, so it is still visible, but it has lost focus so it cannot interact with the user;

3) Stop (Stopped) in Stopped state when activity is covered by another activity and loses focus is not visible;

4) Standby (destroyed) is in destroyed state when it is killed by the system or is not activated.

Activity in a certain state is done by the system, and we cannot control it. However, when the state of an activity changes, we can obtain the relevant notification information through the ONXX () method. Thus, when we implement the activity, we can call them by overwriting the Onxx () method when needed, as shown in 4-1:

Figure 4-1 Activity life cycle

The full life cycle of the activity begins with the first call to the OnCreate () method until the OnDestroy () method is called. The visual life cycle of the activity starts with the OnStart () method call until the application's OnStop () method call ends. During this time, the user can see activity on the screen, although it may not be in the foreground or interact with the user. The activity's foreground life cycle is from the Onresume () method call to the corresponding OnPause () end. During this time, the activity is at the top of the foreground and interacts with the user.

Experience Sharing:

All of this is the life cycle of the activity under normal circumstances. It is important to note that the system may kill a process due to low memory, and all activity will be killed when the process is killed. At this point, OnPause () is the only method that is bound to be called before the process is killed, and OnStop () and OnDestroy () may not be executed.

4.1.2 Activity Stack

We know that we have no control over the state of activity, so what logic does the state of activity operate in? In fact, Android manages activity through an activity stack, and the state of an activity instance determines its position in the stack. The activity at the foreground is always at the top of the stack, and the activity on the second level of the current station is activated and floats to the top of the stack when it is destroyed by an exception or other reason. When the new activity is launched into the stack, the original activity is pushed into the second layer of the stack. A change in the position of an Activity in the stack reflects its transformation between different states. The status of the Activity is shown in relation to its position in the stack 4-2:

Figure 4-2 Activity Stack

The priority of an application is affected by the activity of the highest priority. When deciding whether an application wants to end the release of resources, Android memory management uses stacks to prioritize activity-based applications.

Fourth three cornerstones of Android development-activity, service and handler (1)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.