Android learning notes (1) ---- one of the four Android components (activity)

Source: Internet
Author: User
Tags button type

/*************************************** **************************************** *************
* Author: conowen @ Dazhong
* E-mail: conowen@hotmail.com
* Http://blog.csdn.net/conowen
* Note: This article is original and only used for learning and communication. For more information, indicate the author and its source.
**************************************** **************************************** ************/

1. activity is one of the four Android components (application components). in simple words, activity is the user interface that is commonly seen. Generally, the window occupied by an activity is full screen, but it can also be a small floating window located above other activities. An android project has at least one activity. The activity can contain multiple view instances, such as text boxes, progress bars, check boxes, and buttons. Multiple activities are redirected through intent.

Note: each time you create an activity, you need to inherit the (extends) Activity Class, rewrite the oncreate (bundle) method, and then bind the layout file XML of this activity

Public class secondactivity extends activity {@ override protected void oncreate (bundle savedinstancestate) {// todo auto-generated method stub super. oncreate (savedinstancestate); setcontentview (R. layout. secondactivity); // bind the layout file }}

Finally, you must register the newly added activity in androidmanifest. xml.

<activity            android:label="second"            android:name=".secondactivity" >        </activity>

2. The following is the inheritance relationship between the activity, and the contex class can be seen.

3. Activity Lifecycle

Its lifecycle includes the following types:

Oncreate (), onrestart (), onstart (), onresume (), onpause (), onstop (), ondestroy ()

Note:

Generally, you need to repeat onresume () to update the activity, and then write the Update Processing Method in it.

The difference between the onpause method and the onstop method.

If the second activity does not completely block the first activity, for example, the second activity is in the dialog box format. Only the onpause method of the first activity is called.

When the second dialog box completely blocks the first dialog box, that is, the second activity is full screen, the onpause and onstop methods of the first activity will be called.

In addition, the onresume method is only used to obtain the user focus (in fact, you can click or drag the mouse), that is, the current activity is active.

// Sequence of the first activity to start the second activity first -- onpausesecond activity -- oncreate -- onstart -- onresumefirsr activity -- onstop // full screen status // -- remove the order of the second activity, link second activity -- onpausefirst activity -- onrestart -----------------> and oncreate -- onstart -- onresumesecond activity -- onstop -- ondestroy

4. jump between activities and intent

// The first activitypackage conowen. activity. intent; import android. app. activity; import android. content. intent; import android. OS. bundle; import android. view. view; import android. widget. button; import android. widget. edittext; public class intentactivity extends activity {/** called when the activity is first created. * // @ overridepublic void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate ); Setcontentview (R. layout. main); // create a button and an edit box button bt = (button) findviewbyid (R. id. BT); // locate in main. the button set in XML, // and is also forcibly converted to the button type. When using findviewbyid, the previous type must be enhanced. BT. setonclicklistener (new view. onclicklistener () {// The parameter in setonclicklistener is originally view. onclicklistener, but you can directly add the new class instance in it, and then write the instance to the line @ overridepublic void onclick (view V) {// todo auto-generated method stubintent intent = new intent (); edittext ET = (edittext) findviewbyid (R. id. et); string tempstr = et. gettext (). tostring ();//. the tostring () method returns the string type intent. putextra ("result", tempstr); // Insert the value into the intent and pass it to the second activity. The intent is equivalent to the postman and carries data. // "result ", tempstr indicates a key-value pair. The value of result is tempstrintent. setclass (intentactivity. this, secondactivity. class); // intentactivity is the first activity, and secondactivity is the first activityintentactivity. this. startactivity (intent); // starts to jump to the second activity }});}}

// The second activitypackage conowen. activity. intent; import android. app. activity; import android. content. intent; import android. OS. bundle; import android. widget. textview; public class secondactivity extends activity {@ overrideprotected void oncreate (bundle savedinstancestate) {// todo auto-generated method stubsuper. oncreate (savedinstancestate); setcontentview (R. layout. secondactivity); intent = getintent (); // Get intenttextview textview = (textview) findviewbyid (R. id. textview); string res = intent. getstringextra ("result"); // obtain the corresponding value int resint = integer through the key result of the key-value pair. parseint (RES); // converts a string to an integer textview. settext (resint + ""); // (resint is an integer type) followed by double quotation marks, indicating that all strings in the brackets are strings. // In fact, you can directly write res in the brackets. Res is of the string type and you are familiar with the conversion method }}

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.