Android Basic Activity Article--intent

Source: Internet
Author: User

1. Explicit intent

The intent is used to switch between each activity and can also be used to pass parameters.

Project or use the Activitytest project you created earlier, create a new activity Secondactivity.java, and tick create second_layout.xml.

In Second_layout.xml. Write the code as follows.

<?XML version= "1.0" encoding= "Utf-8"?><LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"android:orientation= "vertical"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent">    <ButtonAndroid:id= "@+id/button_2"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:text= "button 2"     /></LinearLayout>

As has automatically registered the activity in Androidmainfest.xml.

<android:name= ". Secondactivity "></activity>

The Second_layout.xml layout is also automatically introduced in this activity.

Setcontentview (r.layout.second_layout);

2. Add code to the OnClick () method in Firstactivity

Intent intent=New Intent (firstactivity.  this, secondactivity. class ); StartActivity (intent);

Use the intent declaration to create a intent object using the constructor method.

In the Intent () construction method, there are two parameters. The first one is the context, the second is the target activity, the class.

3. Implicit intent

Instead of explicitly specifying the target activity, the system analyzes itself and finally responds to the activity.

4. Add code to Androidmainfest.xml

<ActivityAndroid:name=". Secondactivity ">            <Intent-filter>                <ActionAndroid:name= "Com.example.activitytest.ACTION_START"/> <!--indicates that the activity can be responded to -                <categoryAndroid:name= "Android.intent.category.DEFAULT"/> <!--setting type is Default--            </Intent-filter></Activity>

5. Modify the Click event of a button in firstactivity

Intent intent=New Intent ("Com.example.activitytest.ACTION_START");

Note here that the intent constructor method is an action string.

Note: each Intet object can have only one action, but there may be multiple category.

Intent.addcategory ("Com.example.activitytest.MY_CATEGORY");//Add CATEGORY by this method

Note: an activity can respond only if the action and category are matched. If not the program will crash.

Android Basic Activity Article--intent

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.