Life cycle of activity in Android

Source: Internet
Author: User

One of Android's four main components,--activity, is an integral part of learning about the life cycle of this component. Don't say much, take Google's official API document parsing diagram to see it.

The wirelessly activity is a specific callback function that invokes a series that corresponds to a specific stage in the life cycle. To implement a series of states that are initialized by activiity to destruction. An activity starts with a sequence of callback functions and ends with a series of callback functions. It is important to note that when the system creates an activity, all the callback functions point to the top of the lifecycle, making it visible and interactive.

Start a actiivity:

As you can see from the diagram, an application passes OnCreate ()->onstart ()->onresume ().

* When calling oncreated () indicates that the activity was created, the oncreated () method is only called once throughout the activity life cycle, and the Oncreated method call is instantaneous.

* When calling OnStart () indicates that the activity is already visible, but does not have interactive functions, the OnStart method is instantaneous.

* When calling onresumed () indicates that the activity is visible and interoperable, reaching the top of the pyramid.

This is the process of activity initiation.

Pause and resume an activity:

For example, sometimes when the current activity pops up a dialog box, the current activity is paused. The graph shows that the application resumed the activity through Onresum () after pausing an activity by calling onpaused ().

* When the user focuses on another activityb, the current activity calls onpaused (), at which point the activityb is in a translucent state and the activity is not interactive.

* When the user returns to activity from the current paused state, the user invokes the Onresume () method and resumes the activity.

* Usually in the onpaused () method release some parts that occupy the inner ruler, such as: animation, broadcast, camera, video and so on, and submit the user want to save things, and need to onresume in the parts.

To stop and restart an activity:

When a user switches from your app to another app, your activity stops, or the user receives a call when they use your app, and the activity restarts when the user switches back to your app.

Call Order: onpaused ()->onstop->onrestart->onstart->onresume ()

* Regardless of the way the activity is stopped, you must call onpaused () before calling OnStart ().

* System calls Onrestart () when it is stopped and returned

* When calling the OnStop method, you should perform more intensive operations, such as writing to the database, to the cup.

The content of *onstart () corresponds to the onstop.

Re-create an activity:

When the user presses the return key, or uses Finsh (), or if your activity has been unused for a long time or if the foreground needs more resources, it will destroy your activity. When your activity is destroyed by human action, its instance is permanently destroyed in memory, and when the activity is not artificially destroyed, the instance is destroyed but the memory is still remembered and stored in a key-value pair of the bundle object. This bundle object is typically used to preserve the layout information of the activity. To save additional data about the activity state, you need to overload the onsaveinstancestate () callback method to add a key-value pair to the Bundle object. The system calls this method when the user is ready to leave your activity and passes it to the Bundle object so that your activity can be saved when it is accidentally destroyed . because the OnCreate () method is called when a new instance is created and a previous instance is rebuilt, you must check whether its state is null before you attempt to read a bundle. If it is null, then the system will create a new activity instance instead of re-loading one that was previously destroyed.

Life cycle of activity in Android

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.