Android Learning Note (ii) BASIC components

Source: Internet
Author: User

1. Activity

An activity, usually a separate interface that the user sees on the device at a certain point in time. An application typically contains multiple activity that can be toggled by the user during the period. For the user, this is the appearance part of the program.

Activity life cycle: Starting an activity can consume a lot of resources. He might be involved in creating a new Linux process, requesting memory space for a UI object, populating all objects from a uml layout, and building an entire interface. To avoid this waste, Android manages the life cycle of activities through Activitymanager.

Activitymanager is responsible for creating, destroying, and managing activity.

There are some differences between Android programming and programming in other environments. In Android you will find more responses to changes in the state of the application, rather than actively changing its state. Android is an externally controlled and managed environment that is somewhat similar to programming in Javaapplet and Servleet. So when it comes to the life cycle of activity, we seldom say that his current state is more about the state transition process.

(1) Boot status: OnCreate (), OnStart (), Onresume ()

When an activity is not present in memory, we call it a boot (starting) state. When it starts, the activity invokes a whole series of callback functions that the developer has the opportunity to specify. Last activity hi goes into the running state (running).

Note that one of the most time-consuming actions from the start state to the running state of the transition. The time consuming is the power consumption, also has the direct influence to the battery's endurance ability. So the activity that is no longer displayed is not easily destroyed, but it resides in memory so that the user can save resources the next time they visit.

(2) Operating status

A running activity is an activity that is currently displayed on the screen to interact with the user. We also call this activity the focus (in Foucs), which means that all user interactions-such as typing, touching the screen, and clicking a button-are handled by this activity. It is possible to have only one activity running at a time.

A running activity can take precedence over the memory or resources it needs in order to run as fast as possible.

(3) Pause status

It is paused (paused) when the activity does not have the focus (for example, no interaction with the user) but is still displayed on the screen.

The activity will stop advancing into the paused state. Activities that are paused still have the privilege of acquiring memory or other resources. Because they are still displayed on the screen, they should try to avoid making the user feel unnatural.

(4) Stop State

When an activity is no longer displayed but still resides in memory, we call it a stop state (stopped). An activity in the stopped state can be displayed again, become a running state, or be destroyed and removed from memory.

The system keeps the inactive activity for a while, because the user will likely later want to return to these activities, and restarting a stopped activity is much less expensive than initializing a new activity.

Activities that stop State may be removed from memory at any time.

(5) Destruction status

The Destroy (destroyed) state does not reside in memory. When the activity manager considers that an activity is not needed, the opportunity to remove it is removed. Before the activity is destroyed, he can do certain things, such as saving information. However, Android does not guarantee that your activity will be stopped before it is destroyed, and that suspended activities may be destroyed as well. Therefore, it is a good idea to perform important work when switching back to a paused state.

In fact, the running state does not mean that he is doing frequent operations, he may just be there waiting for the user's input. Similar inactive activities do not necessarily do nothing at all. The state name primarily refers to the extent to which an activity responds to user input, in other words, whether an activity is courseware, gets focus, or is not visible at all.

2, Intent

Intent refers to messages that are passed between major artifacts. It can trigger and start a message that tells a service whether to start or stop, or simply to broadcast. Intent are asynchronous, which means that the code that launches them does not have to wait for them to complete and return.

The intent is divided into explicit (expicit) and implicit (implicit) two kinds. In an explicit intent, the sender needs to explicitly specify the component that receives the intent. In an implicit intent, the sender only needs to specify the type of receiver.

3, Services (service)

The service runs in the background without any user interface. They can do the same thing as an activity, but without any user interface. Services are used for whatever the interface is, and we want to be able to do it over a period of time. For example, if you want to switch between other applications, the music player can still play the song.

The service life cycle is much simpler than the activity. You can start or stop a service, the life cycle of the same service is more or less controlled by the developer and less controlled by the system.

While the service is running in the background, this is not to say that it runs on another separate thread. And if a service is performing a time-consuming operation, you usually need to put it on a separate thread, otherwise it will affect the responsiveness of your user interface. Simply put, services and activities run in the same main application thread, which is also known as the UI thread.

Note: Do not confuse Android services (which are part of the Android English program) with Linux native services, server processes, or daemons (these are the lower-level components of the operating system).

4. Content provider (ContentProvider)

A content provider is an interface for sharing data between applications. Android defaults to each application running in its own sandbox, so that all of the application's data is completely isolated from other programs in the system. While a small amount of data can be passed between different applications through intent, the content provider is used to share persistent data between large datasets. This content provider provides one by one sets of good composite Drud (creat, Read, Update, Delete) principles.

The interface of the content provider is relatively simple, which is the standard insert (), update (), delete (), Qurey () methods. These methods look much like standard database methods, so implementing a content provider for a database is relatively straightforward as a proxy industry. We should use content providers as much as possible instead of implementing them ourselves.

5. Broadcast Receiver (Broadcastreceiver)

Broadcast receivers are implementations of Android at the system level for the "publish-subscribe" mechanism, or the Observer pattern (Observer). The receiver is typically just waiting until the event it subscribes to occurs before it is activated.

The system itself is constantly broadcasting. For example, receive a text message, pick up a phone, and so on. All of these events are broadcast, and they can start any number of receivers.

You can send broadcasts to different parts of your application, or to other completely different applications.

The broadcast itself has no visual representation and is not actively carried out in memory. But once triggered, they may execute certain code, such as starting an activity, service, or other.

6. Application Context (ApplicationContext)

The application context is the process in which the application environment and all its components run. It allows applications to share data and resources in different artifacts.

Regardless of whether an activity, service, or other artifact is first started in the application, the app Chen IPO context is created first. As long as your application does not die, the application context will always survive. Because of this, the application context is independent of the life cycle. You can get a reference to the context by Context.getapplicationcontext () or activity.getapplication ().

Note that both activity and service are subclasses of the context, so they inherit all the methods of the context.

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.