android_2015-04-07 use of intent in Android

Source: Internet
Author: User

1. What is intent

1.1 Intent is a way to interact between components in an Android program, not only to indicate what actions the current component wants to perform, but also to pass data between different components.

Intent can typically be used to initiate events, start services, and send broadcasts, among other scenarios.

2.Intent usage: Two usages of explicit intent and implicit intent

2.1 Explicit Intent, we use the switch between two activities to demonstrate the display intent

2.1.1 Intent has multiple overloads of functions, one of which is

Public Intent (Context Packagecontext, class<?> CLS)

The first parameter is the context that initiates the fetch, and the second parameter is the target activity that you want to start

Create two layout pages first, and create two activities (activity)

MainActivity.cs and SecondActivity.cs

After creating the SecondActivity.cs activity, we want to register the activity in Androidmainifest.xml, just add one line: <activity android:name= ". Secondactivity "></activity>

Activity_main.xml (First layout file) and Second_layout.xml (second layout file)

Activity_main.xml:

      

1 <Relativelayoutxmlns:android= "Http://schemas.android.com/apk/res/android"2 Xmlns:tools= "Http://schemas.android.com/tools"3 Android:layout_width= "Match_parent"4 Android:layout_height= "Match_parent"5 Tools:context= "${relativepackage}.${activityclass}" >6 7    <Button8        Android:id= "@+id/btn01"9 Android:layout_width= "Match_parent"Ten Android:layout_height= "Wrap_content" One Android:text= "Btn01" A        /> -  - </Relativelayout>

Second_layout.xml

    

1 <?XML version= "1.0" encoding= "Utf-8"?>2 <LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"3 Android:layout_width= "Match_parent"4 Android:layout_height= "Match_parent"5 android:orientation= "vertical" >6     7     <Button8         Android:id= "@+id/btn02"9 Android:layout_width= "Match_parent"Ten Android:layout_height= "Wrap_content" One Android:text= "Btn02" A         /> -  - </LinearLayout>

In MainActivity.cs, add the following method to the button Btn01:

    

1Button Button =(Button) Findviewbyid (r.id.btn01);2Button.setonclicklistener (NewButton.onclicklistener () {3 4 @Override5              Public voidOnClick (View v) {6Intent Intent =NewIntent (mainactivity. This, Secondactivity.class);7 startactivity (intent);8             }9});

You will be redirected to SecondActivity.cs when you click Btn01.

In order to show Second_layout.xml we specify the layout page in SecondActivity.cs: Setcontentview (r.layout.second_layout);

      

Younger brother Novice, please teach more

2.2 Implicit intent next section continue

android_2015-04-07 use of intent in Android

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.