Activity of four Android Components

Source: Internet
Author: User

I read an Android intern job application written by a senior student who is going to be a senior student a few days ago and learned some questions about Android, the four components of Android (Activity, Service, Content Provider, BroadcastReceiver broadcast receiver) are the easiest questions to answer. In addition to coping with interviews, four components are required for Android Developers. As a result, Shamoo collected a lot of information on the Internet and sorted it out.

 


Activity Introduction

Activity is a separate interface in the Android application. It displays some controls and can listen to and process user events to respond.

Activities communicate with each other through Intent. In the description structure of Intent, there are two most important parts: the data corresponding to the action and action.

Typical action types include MAIN (activity portal), VIEW, PICK, and EDIT. The data corresponding to the action is represented in URI format. For example, to VIEW the contact information of a person, you need to create an intent with the action type of VIEW and a URI that represents the person.

IntentFilter is related to Intent. As the name implies, IntentFilter is the Intent filter. An Activity can respond to related requests through IntentFilter, and other unrelated requests are filtered out. An IntentFilter is used to describe the intent that an activity (or IntentReceiver) can operate on. IntentFilter must be defined in AndroidManifest. xml. By parsing various intents, it is easy to navigate from one screen to another. When you navigate forward, Activity calls the startActivity (Intent myIntent) method. Then, the system finds the activity that best matches the Intent of myIntent in the IntentFilter defined in all installed applications. The new activity starts to run after it receives a notification from myIntent. When the startActivity method is called, myIntent Parsing is triggered. This mechanism provides two key advantages:

A. Activities can reuse A request generated in Intent form from other components;

B. Activities can be replaced by a new Activity with the same IntentFilter at any time.

The AndroidManifest file contains the following Activity components: default startup class, which is automatically called by the system when the program starts.

 

Intent-filter> <action android: name = "android. intent. action. MAIN "/> <category android: name =" android. intent. category. LAUNCHER "/> </intent-filter> <action android: name =" android. intent. action. MAIN "/> <category android: name =" android. intent. category. LAUNCHER "/> </intent-filter> Activity registration

The registration format in the AndroidManifest file is as follows:

<Activity> the name attribute of the element specifies the subclass of the activity that implements the Activity. The icon and label attributes point to the resource file that contains the icons and labels of the activity displayed to the user.

 


Activity Activation

The Activity is activated by passing an Intent object to Context. startActivity () or Activity. startActivityForResult () to load (or specify a new job to) an activity. The corresponding activity can be viewed by calling the getIntent () method to activate its intent. If it expects the activity it starts to return a result, it will replace startActivity () by calling startActivityForResult (). For example, if it starts another Activity so that users can select a photo, they may want to know which photo has been selected. The result is encapsulated in an Intent object and passed to the onActivityResult () method of the called activity.

 


Activity closed

You can call its finish () method to close an activity.

 


Activity Lifecycle

Tasks are related to the Activity lifecycle. A task is actually the stack of an Activity. It is composed of one or more activities to complete a complete user experience. In other words, a task is an "application" (either one or multiple, for example, you want users to see a street map somewhere. If an activity with this function already exists, you need to put the request information in an Intent object and pass it to startActivity (). The map is displayed on the screen. When you press the BACK key, the previous Activity is displayed on the screen again. At this time, the task is composed of two related activities in the application) the bottom of the stack is the Activity that starts the entire task. The top of the stack is the Activity that the currently running user can interact with. When an Activity starts another activity, the new Activity is pushed to the stack, and become the currently running Activity. The previous Activity remains in the stack. When you press the BACK key, the current Activity goes out of the stack, and the previous Activity is restored to the currently running one. Objects are actually stored in the stack. The Activity in the stack will never be rearranged and only be pushed in or out. Therefore, if multiple street maps are needed, in this case, multiple instances of the same Activity subclass exist simultaneously in a task.

All the activities in the task are moved as a whole. The entire task (that is, the activity stack) can be moved to the foreground or to the background. For example, the current task has four activities in the stack -- three under the current Activity. When you press the HOME key, you return to the Application Loader and select a new application (that is, a new task ). The current task is merged into the background, and the root Activity of the new task is displayed. Then, after a while, the user returned to the Application Loader again and selected the previous application (the previous Task ). So the task, with all the four activities on its stack, once again arrived at the front-end. When the user presses the BACK key, the screen does not show the Activity that the user just left (the root Activity of the previous Task ). Instead, the top Activity in the current task stack is popped up, and the previous Activity in the same task is displayed.


Android is a Multi-Task operating system. You can use your mobile phone to listen to music and execute multiple other programs. Every time you execute an application, it will consume more system memory. when too many programs are executed at the same time, or the closed program does not properly release the memory, the system will feel slower and slower, even unstable.

To solve this problem, Android introduces a new mechanism-Life Cycle ).

The lifecycle of an Android application is managed by the Android framework, rather than directly controlled by the application. Generally, every application (the entry is generally an onCreate method of an Activity) will generate a Process ). When the system memory is about to be insufficient, the process will be automatically reclaimed according to the priority. No matter the user or developer, it cannot be determined when the application will be recycled. Therefore, to prevent data loss and other problems, it is important to understand the lifecycle.

Activity lifecycle:

 
 


Four States, seven important methods, and three nested loops of an Activity throughout its lifecycle


Four statuses:

1. Active/Running status


When the Activity runs on the front-end of the screen (at the top of the current task Activity stack), it obtains the focus to respond to user operations, which is in the running status, at the same time, only one Activity is Active or Running.

2. Paused status

When the Activity loses focus but is still visible to the user (for example, if there is another transparent Activity on it or a pop-up window such as Toast and AlertDialog), it is paused. The paused Activity is still alive (it retains all the status and member information and maintains the connection with the window manager), but can be killed by the system when the system memory is very small.

3. Stopped status

When it is completely blocked by another Activity, it is in the stopped state and retains all the status and member information. It is invisible to users. When memory is needed elsewhere, it is often killed by the system.

4. Dead status

The Activity has not been started, has been manually terminated, or is not active when it has been recycled by the system. To manually terminate the Activity, you can call the "finish" method in the program.

 

7 important methods

When an Activity enters another State from one state, the system will automatically call the following method to notify the user of this change. When the Activity is first instantiated, the system will call it, this method is called only once throughout the lifecycle.


Usually used for initialization settings: 1. Set the layout file to be used for the Activity; 2. Bind the listener to the button and other static settings; Use onCreate (Bundle savedInstanceState );

The system calls onStart () when the Activity is visible and does not obtain the user focus and cannot interact ();

When the Activity has been stopped and restarted, the system will call onRestart ();


The system calls onResume () when the Activity is visible and the user focus is interactive ();


When the system starts another Activity, it is called by the system to save the persistent data in the existing Activity and stop the animation before the new Activity starts. This implementation method must be very fast. After the Activity is disabled, the system does not recycle the memory. The user will expect that when he returns to this activity again, it will remain like the last time he left. At this time, onSaveInstanceState () is used. The onSaveInstanceState () method is used to save the status before the Activity is killed and is triggered before onPause, this method needs to be rewritten when the system destroys the Activity to save memory (the user does not want to destroy the Activity). When the Activity is instantiated again, it will pass onCreate (Bundle savedInstanceState) pass stored temporary status data into the system because the onSaveInstanceState () method is not always called. The trigger condition is (press the HOME Key, press the power button to close the screen, and switch between the screen ), you should only override onSaveInstanceState () to record the temporary status of the Activity, rather than persistent data. OnPause () should be used to store persistent data.


OnStop () is called by the system when the Activity is completely overwritten and invisible by the new Activity ();


When the Activity (the user calls finish () or the system is killed by the system due to insufficient memory), the system calls onDestroy () (only one call is called throughout the lifecycle) to release onCreate () resources created in the method, such as the end thread.

 

3 nested loops

1. complete lifecycle of the Activity: From the first onCreate () call to the end of onDestroy () call

2. Visible life cycle of an Activity: From onStart () to onStop ()

Between the two methods, you can maintain the resources required by the Activity. For example, register a broadcast receiver listener in onStart () to monitor changes that affect your UI and log out in onStop.

3. Foreground life cycle of an Activity: From onResume () to onPause ().

 


For example:


There are Three acitifications, which are expressed by One, Two (transparent), and Three. One is the main Activity when the application is started.

When the first Interface Activity One is started, its order is

OnCreate (ONE)-onStart (ONE)-onResume (ONE)

When you click "open transparent Activity", the order is

OnPause (ONE)-onCreate (TWO)-onStart (TWO)-onResume (TWO)

Click back to return to the first interface. The order in which Two will be killed is

OnPause (TWO)-onActivityResult (ONE)-onResume (ONE)-onStop (TWO)-onDestroy (TWO)

Click "enable full screen Activity". The order is

OnPause (ONE)-onCreate (Three)-onStart (Three)-onResume (Three)-onStop (ONE)

Click back to return to the first interface. Three will be killed. The order is

OnPause (Three)-onActivityResult (ONE)-onRestart (ONE)-onStart (ONE)-onResume (ONE)-onStop (Three)-onDestroy (Three)

<Supplement> If the program is mounted to the background by HOME, the order is

OnSaveInstanceState ()-onPause (ONE)-onStop (ONE)

<Supplement> return from the background. The order is

OnRestart (ONE)-onStart (ONE)-onResume (ONE)

When you click back to exit the application, the order is

OnPause (ONE)-onStop (ONE)-onDestroy (ONE)

 

 


 

Related Article

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.