Four components of the Android development Learning Note---Activity introduction, creation, and life cycle

Source: Internet
Author: User

Recently re-review the basics of Android development, or share the blog, on the one hand to share to the needy students, on the other hand to facilitate their later viewing

I. What IS activity

In layman's terms, a screen interface is an activity, and a comparison with the Department of Education, in an Android application, an activity is usually a separate screen, which can show that some controls can also listen and handle the user's events to respond. Activity is communicated through intent, and all operations in the activity are closely related to the user, a component that is responsible for interacting with the user , and the specified control can be displayed through Setcontentview (View).

Second, how to create an activity

Creating an activity in your code can be divided into three steps, that is, an activity that requires three components

The first part is the Activity Interface section, the interface file is an XML file, in this file we can adjust the layout of the activity, add a variety of controls and so on, the layout file is located in the Project Res/layout folder, open this file, functions such as:

You need to add a control, select the control you want to add in the left control list, left-click to drag to the right-hand view interface, and the control will be added to the activity layout, for example, the button in the interface is the one that I added, let's take a look at the code:

There is an OnClick property in the button this is something, which we'll introduce later, and here we'll create a UI part of the activity.

In the second part, we need to create the logical part of the acitivity, implement the part, and associate the logical interaction class with the UI, and the code path of the logical part is basically created in each package under SRC, so let's look at the code:

There are two ways to add a click event to a control, the first is to write a click event method, add the OnClick property to the control property to specify the second one to create a click event Listener, and implement the listener's OnClick method, Then use the Setonclicklistener method of the control to bind the listener to the corresponding control, with the following code:
 Public classStartextendsandroid.app.activity{@Override Public voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);    Setcontentview (R.layout.main); Button BTN1= (Button) Findviewbyid (r.id.starttest);//positioning ControlsBtn1.setonclicklistener (my);//to bind a listener to a button }//instantiate a listener and rewrite the onclick PrivateOnclicklistener my=NewOnclicklistener () {@Override Public voidOnClick (View v) {//TODO auto-generated Method StubLOG.E ("Baih", "======================================================="); Runtime Run=Runtime.getruntime (); Try{run.exec ("AM Instrument--user 0-w suileytest.baih/baih. Instrumentationtestrunner "); } Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace (); }    }};}

The third part, now the logical realization has, the interface has, we need to put this activity in manifest registration, manifest is the entire program's configuration file, if the component is not registered in it, the program is not recognized, the registration method is as follows:

<activity
Android:name= ". Mainactivity "
Android:label= "@string/app_name" >
<intent-filter>
<action android:name= "Android.intent.action.MAIN"/>

<category android:name= "Android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>

<activity
Android:name= ". Eractivity "
Android:label= "ER" >
</activity>

A new Activity node is added under the application node in the manifest, with the properties of the activity, which is registering an activity, which includes the name (the name of the class that fills in the activity), and the label ( Activity title), and claims that you can use Intent-filter for permissions

At this point, an activity is created, and when you run the program, the activity is displayed and can be easily interacted with.

Four components of the Android development Learning Note---Activity introduction, creation, and life cycle

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.