Android Activity Summary

Source: Internet
Author: User

Http://www.cnblogs.com/lyp3314/archive/2011/11/10/2244971.html

I. What IS activity

 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,

Second, activity life cycle

  

View Code

The graph shows:

During the normal startup of an activity, these method calls are in the order of OnCreate, OnStart, and Onresume; The method sequence is OnPause, OnStop, when the activity is killed. OnDestroy, this 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.

The role of the method in the system and what we should do:

onCreate: Create the interface here, do some data initialization work;

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 is 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 is running in the thread, please cancel it in OnDestroy, or else the Cancel method called dialog will throw an exception when it ends.

Onpause,onstop, OnDestroy, in three different states, the activity can be killed by the system.

III. communication between the 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.

Iv.Intent Filter for Ac tivity

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:

V. On-the-heap management of activity

Android's management of activity uses stacks, meaning that only one activity at a time is at the top of the stack, and when the activity is destroyed, the following activity is likely to float to the top of the stack, or a new activity is created, The old activity was sunk by the stack. Activity is the performance layer of the Android program. Each display screen of a program is an activity. The activity that is running is at the top of the stack and it is in the running state.

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.

Vi. Loading 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 all create only one instance, when intent arrives, need to create the activity that is set to Singletask, The system checks to see if an instance of the activity is already in the stack. If there is a direct send intent to it.

SingleInstance:

Starting with the concept of task, a task can be thought of as a stack that 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.

Vii. Activity's Jump

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 that the current activity is not responding. I am a novice, no experience, such as the discovery of errors or any good advice, please correct me! Please give me a lot of advice!

Android Activity Summary

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.