2.APP components-activities

Source: Internet
Author: User

1. Activities

An was an Activity application component that provides a screens with which users can interact in order to do something, such a s dial the phone,

Take a photo, send an email, or view a map. Each activity was given a window in which to draw its user interface. The window typically fills the

Smaller than the screen and floats on top of other windows.

2. Creating an Activity

3. Implementing a user interface

The user interface for an activity are provided by a hierarchy of views-objects derived from the View class. Each view controls a particular

Rectangular space within the activity ' s window and can respond to user interaction. For example, a view might is a button that initiates

An action when the user touches it.

You can set setContentView() the layout as the UI for your activity with, passing the resource ID for the layout.

4. Declaring the activity in the manifest

<manifest >  <application ... >      <activity android:name=". Exampleactivity" /> ...   </application ... > ...   </manifest >
View Code

5. Using Intents Filters

An <activity> element can also specify various intent filters-using the <intent-filter> element-in order to declare how other Applicati On

Components may activate it.

<activity android:name=". Exampleactivity "android:icon="@drawable/app_icon">    < intent-filter>        <action android:name="android.intent.action.MAIN" />        <category android:name="android.intent.category.LAUNCHER" />    </intent-filter></activity>
View Code

<action>The element specifies the "main" entry point to the application.

The <category> element specifies that this activity should is listed in the system's application Launcher (to allow users to LA Unch this activity).

6. Starting a Activity

Intent Intent =NewIntent (); Intent.setclass (mainactivity. This, Newclass.class); Bundle Bundle=NewBundle (); Bundle.putstring ("Name","Zhangsan"); Bundle.putstring (" Age","Ten"); Intent.putextra (bundle); StartActivity (intent);
View Code

In the Newclass.class,

// newclass.getintent ()  This  = bundle.getstring ("Name"= bundle.getstring (" Age " );....
View Code

7. Starting an activity for result

Sometimes, might want to receive a result from the activity so you start. In this case, start the activity by callingstartActivityForResult()

(instead of startActivity() ). To then receive the result from the subsequent activity, implement the onActivityResult() callback method.

When the subsequent activity is do, it returns a result in an to Intent your onActivityResult() method.

Private voidpickcontact () {//Create an intent to ' pick ' a contact, as defined by the content provider URIIntent Intent =NewIntent (Intent.action_pick, Contacts.content_uri); Startactivityforresult (Intent, pick_contact_request);} @Overrideprotected voidOnactivityresult (intRequestcode,intResultCode, Intent data) {    //If The request went well (OK) and the request were pick_contact_request    if(ResultCode = = Activity.result_ok && Requestcode = =pick_contact_request) {        //Perform a query to the contact's content provider for the contact ' s namecursor cursor =getcontentresolver (). Query (Data.getdata (),NewString[] {contacts.display_name},NULL,NULL,NULL); if(Cursor.movetofirst ()) {//True If the cursor is not empty            intColumnIndex =Cursor.getcolumnindex (contacts.display_name); String name=cursor.getstring (columnindex); //Do something with the selected contact ' s name ...        }    }}
View Code

8. Shutting down an Activity

You can shut a activity by calling it finish() method.

9. Activity LifeCycle

  

Ten. Saving Activity State

The system calls onSaveInstanceState() before making the activity vulnerable to destruction. The system passes this method a Bundle in which

You can save state information about the activity as Name-value pairs, using methods such putString() as and c3> putInt() . Then, if the

System kills your application process and the user navigates back to your activity, the system recreates The activity and passes the

    BundleTo both onCreate() onRestoreInstanceState() and. Using either of these methods, you can extract your saved

    Bundleand restore the activity state. If There is no state information to restore and then the Bundle passed to are null (which is the case

When the activity was created for the first time).

    

Coordinating activities

The order of lifecycle callbacks is well defined, particularly when the "the" and "activities" is "in the same process" and "one is" Starting the other.

Here's the order of operations that occur when Activity A starts acivity B:

1. Activity A ' s onPause() method executes.

2. Activity B ' s onCreate() , onStart() , and onResume() methods execute in sequence. (Activity B now has user focus.)

3. Then, if Activity A was no longer visible on screen, its onStop() method executes.

  

2.APP components-activities

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.