Getting Started with Android Development Note # Early recognition activity

Source: Internet
Author: User

I. Basic concept of activity

Activity is an important component in Android development, which provides users with basic screen interaction space. Simply put, we intuitively see the application-operable interface, which is based on managed activity.

An application usually consists of multiple activity, one activity can start another acitvity, the new activity is called the child activity, the parent activity will automatically stop, and be pressed into the stack, when the child activity returns, The parent activity is then ejected from the stack.

Ii. life cycle of activity

Official Android documentation There is a saying: it is particularly important to develop a powerful and flexible application that implements the callback method of the activity to manage its lifecycle.

Therefore, the whole life cycle of acitvity is the foundation of the later development of complex application.

1. Life cycle Control

The full life cycle of the activity is all the processes from OnCreate () to OnDestroy (), such as:

The life cycle of the activity is primarily hosted by the Android system. Typically only start (StartActivity ()) and Shut Down (Finish ()) Activity can be controlled by developers.

Other OnXxx () methods are called by the Android system according to different external conditions, and developers cannot participate directly in the control.

That is, the developer does not control the transformation of the life cycle, but we can control the performance of the application over the life cycle through the callback method OnXxx ().

So, developers need to know:

    • When the life cycle will change
    • What can we do when there is a change

2. When the life cycle changes and what we can do

    • The full life cycle of the activity occurs between OnCreate () and OnDestroy () calls. Your activity should be in the OnCreate () method to complete all the "global" state settings (for example, define layout), and in the OnDestroy () method, release all the resources occupied. For example, if your activity has a thread running in the background that is used to download data from the network, you should create the thread in the OnCreate () method and stop the thread in the OnDestroy () method.
    • The visible lifetime of the activity occurs between the OnStart () call and the OnStop () call. During this time, users can see the activity on the screen and interact with it. For example, when a new activity is started and the OnStop () method is called, the activity cannot be seen. Between these two methods, you can manage the resources needed to display the activity. For example, you can register a broadcastreceiver in the OnStart () method to monitor changes that affect the user interface. And when the user no longer sees your display, write it off in the OnStop () method. The system calls OnStart () and OnStop () multiple times throughout the lifetime of the activity, as activity may be constantly switching back and forth between display and hide.
    • The activity's foreground lifetime occurs between the Onresume () call and the OnPause (). During this time, the activity is positioned before all other activity on the screen and has the user's input focus. Activity can enter and exit the foreground frequently-for example, OnPause () is called when the device enters hibernation or when a dialog box pops up. Because this state can occur frequently, the code in both methods should be fairly lightweight in order to avoid user waits caused by switching delays.

Visible lifetimes and foreground lifetimes seem a bit confusing, specifically:

The activity may be in three basic states:

resumed
activity is in the foreground of the screen and has the focus of the user. (This state is sometimes called "running".)
Paused
Another activity is in the foreground and has the focus, but this activity is still visible. That is, another activity is covered by the activity, and the activity is partially transparent or does not cover the entire screen. A paused activity is completely alive (the activity object remains in memory, it maintains all the state and member information, and remains connected to the window manager), but it can be killed if the system memory is critically low.
Stopped
This activity is completely obscured by other activity (this activity is currently in the background). A stopped activity is still alive (the activity object remains in memory, and it maintains all the state and member information, but no longer joins with Window manager). However, it is no longer visible to the user and will be killed when memory is needed elsewhere.

If the activity is paused or stopped, the system can remove it from memory by requesting finish (calling its finish () method) or directly killing its process. When the activity is started again (after completion or kill), it must be completely rebuilt.

Getting Started with Android Development Note # Early recognition activity

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.