Talking about Android Activity

Source: Internet
Author: User

Activity is the most important, most common, and most basic component of Android, and below is a detailed description of the activity.

Introduced

A acitvity as a user interface displayed on the screen, such as in an e-mail application such as an activity to display a collection list, an activity to write a message, an activity to read a message, and so on. Activity is used to provide a user experience, and the activity of many different experiences gathers together to create a user experience for an Android application, each of which is independent of each other. In addition to accessing your activity, the app can also access other apps ' acitivity (which need to be allowed by the app).

Second, the activity class indirectly or directly inherits the base classes such as content,contextwrapper,contextthemewrapper, so activity can call their methods directly. In order for the service to respond to user requests the developer needs to rewrite Httpservice's dorequest (...), Doresponse (.. ) method, or override service (...), activity is similar, creating an activity also requires implementing one or more methods, the most common of which is implementing OnCreate (Bundle status), The method is called back when the activity is created, calling the activity's Setcontentview (view view) method to display the view to be displayed, in order to manage the components in the application, invoke the activity's Findviewbyid ( int id) method to get the components in the program interface.

Life cycle of activity

The boot order of an activity:

OnCreate ()-->onstart ()-->onresume ()

When another activity starts:

First activity OnPause ()--second activity onCreate ()-->onstart ()-->onresume ()

--First activity onStop ()

When you return to the first activity:

Second activity OnPause ()--First activity Onrestart ()-->onstart ()-->onresume ()

--second activity onStop ()-->ondestroy ()

The order in which an activity is destroyed:

(Condition i) onPause ()--><process killed>

(Situation II) OnPause ()-->onstop ()--><process killed>

(Situation III) OnPause ()-->onstop ()-->ondestroy ()

Each activity is in a certain state, and for the developer it is impossible to control its application in one state, which is done by the system.

However, when the state of an activity changes, the developer can obtain the relevant notification information by calling ONXX ().

When implementing the Activity class, these methods can be called by overwriting (override) when you need to process them.

OnCreate: When the activity starts for the first time, the method is triggered, at which time the activity initialization can be completed.

The OnCreate method has a parameter that can be empty (null), or it can be a state information that was previously saved by calling the Onsaveinstancestate () method.

Second, OnStart: The trigger of the method indicates that the activity will be displayed to the user.

Onresume: When an activity interacts with the user, the method is triggered.

Iv. OnPause: Triggers This method when an activity that is running in the foreground is running in the background because other activities require the foreground to run. This is the time to persist the state of the activity, such as the database record being edited.

V. OnStop: Triggers the method when an activity no longer needs to be presented to the user. If the memory is tight, the system will end the activity directly without triggering the OnStop method. So saving state information should be done at onpause time, not onstop. Activities that are not running in the foreground will be stopped or the Linux management process can end these activities at any time in order to reserve enough storage space for the new activity. Therefore, it is important for developers to always keep this principle in mind when designing applications. In some cases, the OnPause method may be the last method of activity triggering, so developers need to save the information at this time.

Onrestart: Triggers the method when an activity in the stopped state needs to be presented to the user again.

Vii. OnDestroy: Triggers the method when the activity is destroyed. As with the OnStop method, if the memory is tight, the system will end the activity directly without triggering the method.

· Onsaveinstancestate: This method is called by the system, allowing the activity to save its previous state, such as where the cursor is located in a string of strings.

Typically, developers do not need to override this method, and in the default implementation, all state information for the user interface components involved in the AutoSave activity has been provided.

methods associated with the activity life cycle

In the android.app.Activity class, Android defines a series of life-cycle related methods that, in our own Activity, only need to be replicated as needed, Java Polymorphism will ensure that our own method is called by the virtual machine, which is similar to the MIDlet in J2ME.

public class Ouractivity extends Activity {     protected void onCreate (Bundle savedinstancestate);     protected void OnStart ();     protected void Onresume ();     protected void OnPause ();     protected void OnStop ();     protected void OnDestroy ();  

The instructions for these methods are as follows:

    1. protected void OnCreate (Bundle savedinstancestate) is the first method that is called when an Activity instance is started. In general, we cover this method as an entry point for the application, where we do some initialization data, set up the user interface, and so on. In most cases, we're going to load a well-designed user interface from XML here. For example:

setContentView(R.layout.main);

Of course, we can also read the data we saved to the storage device from savedinstancestate , but we need to determine if savedinstancestate is nullbecause the Activity No data is stored on the device at the first boot:

if (savedinstancestate!=null) {  savedinstancestate.get ("Key");  }

    1. protected void OnStart () The method is called after the OnCreate () method, or when the Activity transitions from the Stop state to the active state.

    2. protected void Onresume () is called when the Activity transitions from the Pause state to the active state.

    3. protected void OnPause () is called when the Activity transitions from the active state to the Pause state.

    4. protected void OnStop () is called when the Activity transitions from the active state to the Stop state. In general, we save the status information of the Activity here.

    5. protected void OnDestroy () is called at the end of the Active operation, which is the last method called at the end, where it is generally done to release resources, clean up memory, and so on.

Activity Status Conversion diagram

as shown above, Android programmers can determine the "life" of an activity, but cannot determine its "death", it is said that the programmer can start an activity, but can not manually "end" an activity. When you call the Activity.finish () method, the result is the same as when the user presses the back key: tells the activity Manager that the activity instance has completed the corresponding work and can be "recycled" [according to the meaning below, The finish method should be called only to bring activity into the stoped State]. Activity Manager then activates the activity on the second level of the stack and re-enter the stack, while the original activity is pressed into the second layer of the stack, from the active state to the Paused state. For example: Activity2 is started from Activity1, then Activity2 is currently at the top of the stack, and the second layer is Activity1, when we call the Activity2.finish () method, the Activity The Manager reactivated the Activity1 merge stack, Activity2 transitions from the active state stoped state,Activity1. Onactivityresult (int requestcode, int Resultco The DE, Intent data) method is executed, and the Activity2 returned is returned to Activity1 by means of the database parameter.

Create a process for activity

First, create a Activit

Y

after an activity is created, in order for it to be accessible the system must declare it to be registered in the Androidmanifest.xml text of the application :

Overriding the OnCreate method

OnCreate (), when the activity is first started, the method is triggered, and the initialization of the activity can be completed at this point, which must be overridden. The system calls this method to create the activity, and implementing this method is an important step in initializing the activity you created. The most important of these is to call Setcontentview () to define the layout of the user interface you want to present.

Talking about Android Activity

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.