Activity of Android four basic components

Source: Internet
Author: User

"Activity Introduction"

Activity is a user interface program, in principle it provides the user with an interactive interface function. It is the basic functional unit of an Android application. The Activity itself has no interface. So the activity class creates a window where the developer can put the UI on the activity-created window via the Setcontentview (View) interface, and when the activity points to a full-screen window, It can also be implemented in other ways: as a floating window (via Windowisfloating's Theme collection), or embedded in other activity (using Activitygroup). The activity is separate and is used to handle user actions. Almost all the activity has to do with the user.

An application is usually composed of multiple activities, and they are usually loosely coupled. Typically, activity in an application is designated as "main" activity, which is presented to the user when the application is first launched. Each activity can then start another activity in order to complete a different action. Each time an activity starts, the previous activity stops, but the system retains activity on a stack ("Back stack"). When a new activity is started, it is pushed to the top of the stack to get the user focus. The back stack conforms to the simple "last in, first out" principle, so when the user finishes the current activity and then clicks the Go button, it is popped up (and destroyed) before the activity resumes.

When an activity is stopped due to a new activity initiation, it is notified that this state transitions through the activity's lifecycle callback function. There are many callback functions an activity may receive, originating from its own state change-whether the system creates it, stops it, restores it, destroys it-and each callback provides you with the opportunity to complete the specified work that fits the state. For example, when you stop, your activity should release any large object, such as a network database connection. When activity resumes, you can regain the necessary resources and restore the interrupted action. These state transitions are part of the activity's life cycle.

"Activity life cycle"

public class Activity extends ApplicationContext {

protected void OnCreate (Bundle savedinstancestate);

protected void OnStart ();

protected void Onrestart ();

protected void Onresume ();

protected void OnPause ();

protected void onStop ();

protected void OnDestroy ();

}

During the normal startup of an activity, the order of these method calls is OnCreate, OnStart, and Onresume;

When the activity was killed, the method sequence is OnPause, OnStop, OnDestroy, which is a complete lifecycle.

Then for interrupt processing (such as the phone), it is OnPause-onStop, recovery OnStart-onresume; If the current application is a theme (translucent) or translucent Activity so interrupted is onpause, recovery time onresume.

Then for "Other app need Memory", is our mobile phone in the running of an application, it is possible to call in the phone to send in text messages, or no electricity, this time the program will be interrupted, priority to the basic function of the service phone, and the system does not allow you to occupy too much resources, At least some features (such as the phone) must be guaranteed, so when resources are low, they can be killed.

"Methods in Activity"

onCreate: Create the interface here, do some initialization of data, you can put the UI through the Setcontentview (View) interface to the activity created by the window

OnStart: To this step becomes "user visible non-interactive" state;

Onresume: Become and user-interactive , (in the activity stack system through the stack of the activity, that is, the current activity at the top of the stack, run out of pop-up stack, then back to the previous activity);

OnPause: To this step is visible but not interactive, the system will stop the animation and other CPU-consuming things. As you know from the above description, you should save some of your data here, because your program's priority is reduced and may be withdrawn by the system. The data that is stored here should be read in the Onresume.

onStop: becomes invisible and is covered by the next activity

OnDestroy: This is the last time the activity was killed before the method was called, perhaps other classes call the Finish method or the system in order to save space to temporarily kill it, you can use isfinishing () to judge it, If you have a progress dialog running in the thread, please cancel it in OnDestroy, or the Cancel method called dialog will throw an exception when the threads end. Onpause,onstop, OnDestroy, in three different states, the activity can be killed by the system.

"Communication in Activity"

In Android, different Activity instances may run in a process, or they may run in different processes. So we need a special mechanism to help us deliver messages between the Activity. In Android, a message is represented by a Intent object, a Intent object that contains not only the destination of the message, but also the content of the message, which is like an email, which should include not only the address of the recipient, but also the specific content. For a Intent object, the message "destination" is required, while the content is optional.

Intent is responsible for the operation of the action, the action involved in data, additional data description, Android based on this intent description, is responsible for finding the corresponding component, the intent passed to the calling component, and complete the call of the component. As a result, intent plays a role as a media intermediary, specifically providing information about the components that are called to each other, and decoupling the caller from the callee.

In the application, we can use intent in two ways:

Direct intent: Specifies the intent of the component attribute (called SetComponent (componentname) or SetClass (Context, Class). Notifies the app to launch the corresponding component by specifying a specific component class.

Indirect intent: No intent is specified for the Comonent property. These intent need to contain enough information so that the system can determine the components that meet this intent in all available components, based on this information.
For direct intent,android There is no need to parse because the target component is already clear.

What Android needs to parse is those indirect intent that, through parsing, map intent to activity, intentreceiver, or service that can handle this intent. The intent parsing mechanism is primarily to find a matching intent by locating all intentfilter in the androidmanifest.xml and the intent defined therein.

"Intent Filter in Activity"

Intent Filter describes what kind of Intent objects a component is willing to receive, and Android abstracts it into a Android.content.IntentFilter class. In Android's androidmanifest.xml configuration file, you can specify its intent filter for an Activity via the <intent-filter > node to tell the system that the Activit Y can respond to what type of Intent.

When you use StartActivity (intent) to start another Activity, if you specify the Component property of the intent object directly, Activity Manager will attempt to start the Act specified by its Component property Ivity. Otherwise, Android will find the most matching boot from all the activity installed in the system via the other properties of Intent, and the application will get an exception thrown by the system if no suitable activity is found. The process for this match is as follows:

"On-the-heap management of activity"

When the Activity.finish () method is called in a program, the result is the same as when the user presses the back key: It tells the activity manager that the activity instance can be "recycled." Activity Manager then activates activity on the second level of the stack, pressing the original activity into the second layer of the stack, from the Running state to the Paused state.

"Load mode of activity"

Standard, Singletop, Singletask, SingleInstance (where the first two are a group, the latter two are a group), the default is standard

Standard: The intent will be sent to the new instance, so each jump will generate a new activity.

Singletop: A new instance is also sent, but a different standard is that when the requested activity is at the top of the stack (configured as singletop activity), no new instances are constructed

Singletask: And the subsequent singleinstance only create an instance, when the intent arrives and needs to create the activity that is set to Singletask, the system checks to see if there is an instance of the activity in the stack. If there is a direct send intent to it.

SingleInstance: First, the concept of task, which can be thought of as a stack, can be put into multiple activity. For example, to launch an app, Android creates a task and then launches the app's entry activity, which invokes the other activity on its interface only in this task. What if you share an activity in multiple tasks? For example, if you open a Guide service application, there is an activity is to open Google Maps, when the home button to return to the main menu and launch Google Maps app, the display is just the map, is actually the same activity, In fact, this introduces the singleinstance. SingleInstance mode is to put the activity on a separate stack, so that only this activity in this stack, different applications of the intent are received and displayed by this activity, so that the sharing. Of course, the premise is that these applications are not destroyed, so just pressed the home button, if the return key is pressed, it is not valid.

"Jump to Activity"

Activity jump, no return results

This is the simplest way to jump the activity. Starting another activity from one activity, direct startactivity (new Intent (current activity.this, next activity.class)).

Activity jump, return data/Results

If you need to return data or results, use Startactivityforresult (Intent Intent, int requestcode), and the value of Requestcode is custom and used to identify the target activity of the jump. The target activity of the jump is to return the data/result, setresult (int resultcode) returns only the result without data, or setresult (int resultcode, Intent data) both return! The processing function that receives the returned data/result is onactivityresult (int requestcode, int resultcode, Intent data), Here the Requestcode is Startactivityforresult Requestcode,resultcode is setresult inside of the ResultCode, the data returned inside data.

* * Note that after Setresult, to call Finish () to destroy the current activity, or not return to the original activity, you cannot perform the original activity of the Onactivityresult function, See the current activity is not responding

Activity of Android four basic components

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.