Android Four components

Source: Internet
Author: User

Organize the life cycle of Android's four components, bibliography: The first line of code

Android Four components
    • Activity
    • Service
    • Broadcastreceiver
    • ContentProvider

      Life cycle of activity:
      Android uses tasks to manage activities, and a task is a set of activities that are stored in the stack, also known as the back stack. A stack is a last-in-first-out data structure that, by default, is placed on the stack in the return stack and at the top of the stack whenever a new activity is enabled. When you press the back key or call the finish () method to destroy an activity, the activity at the top of the stack goes out of the stack, and the previous activity on the stack is re-positioned at the top of the stack. The system always displays the activity at the top of the stack to the user.

Shows how the return stack manages the operation of the activity into the stack stack:

Lifetime of activity:

    • OnCreate (): Called when the activity is first created, and in this method, the activity fee initialization is done, such as loading the layout, binding events, and so on.
    • OnStart (): Called when the active OU cannot be added to the invisible.
    • Onresume (): Called when the activity is ready to interact with the user. The activity at this point must be at the top of the stack that is returned and is in a running state.
    • OnPause (): This method is called when the system is ready to start or resume another activity. Typically, some of the GPU-consuming resources are freed up in this method to hold some key data.
    • OnStop (): This method is called when the activity is completely invisible. The difference between him and the Onpuse () method is that if the new activity started is a dialog-box activity, then the OnPause () method will be executed, and the OnStop () method will not execute.
    • OnDestroy (): This activity is called before it is destroyed, and then the active state becomes the destroy state.
    • Onrestart (): This method is called before the activity is changed from the stop state to the running state, i.e. the activity is restarted.

Process:

Life cycle of Service
When a project's any where invokes the context's StartService () method, the corresponding service starts up and callbacks the Onstartcommand () method. The OnCreate () method executes before the Onstartcommand () method if the service is not created. After the service is started, it remains running until the StopService ()/Stopself () method is called. * Although the StartService () method is called every time, Onstarcommand () executes once, but there is actually only one instance per service. So, no matter how many times the StartService () method is called, all you need to do is call the StopService () or Stopself () method and the service will stop!
In addtion, you can also call the context's Bindservice () method to get a persistent connection to a service that will callback the Onbind () method in the service. Similarly. If the service is not created, the OnCreate () method executes first. The caller can then obtain an instance of the IBinder object returned in the Onbind () method.
Once the StartService () method is called, the StopService () method is called, and the OnDestroy () method in the service executes, indicating that the service has been destroyed. Similarly, when the Bindservice () method is called and the Unbindservice () method is called, the OnDestroy () method is also executed.
When the StartService () method is called on a service and the Bindservice () method is called, the StopService () and Unbindservice () methods are called at the same time to execute.

The life cycle of Broadcastreceiver:
The life cycle is only about 10 seconds, if within onreceive () more than 10 seconds of things, it will be reported: Application no response program unresponsive error message, his life cycle from the callback OnReceive () method to the end of the method return results.
The life cycle of ContentProvider:
Steps to create ContentProvider:

      • OnCreate (): used when initializing the content provider. Operations such as creating and upgrading a database are usually done here, and returning True indicates that the content provider was initialized successfully and that returning false indicates a failure.
      • Query (): Queries the data from the content prompt. Use the URI parameter to determine which table the query is in, the projection parameter determines which columns are queried, the selection and Selectionargs parameters are used to constrain which rows are queried, and the SortOrder parameter is used to sort the results. The results of the query are put back in the cursor object.
      • Insert (): Updates the data already in the content provider. Use the URI parameter to determine the table to add, and the data to be added is saved in the values parameter.
      • Update (): The updated content provides the data already in it, uses the URI parameter to determine which data in the table is more rows, the new data is saved in the values parameter, the selection and Selectionargs parameters are used to constrain which rows are deleted, and the number of rows deleted is returned as the return value.
      • GetType (): Returns the corresponding MIME type based on the content URI passed in.

Android Four components

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.