Android development-Intent

Source: Internet
Author: User

1. What is intent?

Intent Chinese meaning is the purpose. In Android, it also means "purpose. This is where we are going. Intent is needed to go from this activity to another activity.

ExampleCodeI:

 
1: // Define an intent
 
2:Intent intent =NewIntent (intentdemo.This, Anotheractivity2.Class);
 
3: // Start the activity
 
4:Startactivity (intent );

The preceding sample code is used to switch from intentdemo activity to anotheractivity2. This is one of intent constructor methods, specifying two activities. Why do we need to specify two activities? Because there is an active stack in Android, this construction method can ensure that the previous activity is correctly pushed into the stack, and the activity can correctly exit the stack when the return key is triggered.

Note: all activities must be declared in androidmanifest. xml. Which is used in this article?ProgramConfiguration File

1: <?XML Version= "1.0" Encoding= "UTF-8"?>
 
2: <Manifest Xmlns: Android= "Http://schemas.android.com/apk/res/android"
 
3:Package= "Com. halzhang. Android. intent" Android: versioncode= "1"
 
4:Android: versionname= "1.0">
 
5:<Application Android: icon= "@ Drawable/icon" Android: Label= "@ String/app_name">
6:<Activity Android: Name= ". Intentdemo" Android: Label= "@ String/app_name">
 
7:<Intent-Filter>
 
8:<Action Android: Name= "Android. Intent. Action. Main" />
 
9:<Category Android: Name= "Android. Intent. Category. launcher" />
 
10:</Intent-Filter>
 
11:</Activity>
12:<Activity Android: Name= ". Anotheractivity" Android: Label= "Another">
 
13:<Intent-Filter>
 
14:<Action Android: Name= "Android. Intent. Action. Edit" />
 
15:<! -- Category must be configured; otherwise, an error occurs: activity cannot be found -->
 
16:<Category Android: Name= "Android. Intent. Category. Default" />
 
17:</Intent-Filter>
18:</Activity>
 
19:
 
20:<Activity Android: Name= ". Anotheractivity2" Android: Label= "Another2">
 
21:<Intent-Filter>
 
22:<Action Android: Name= "Android. Intent. Action. Edit" />
 
23:<Category Android: Name= "Android. Intent. Category. Default" />
 
24:</Intent-Filter>
25:</Activity>
 
26:</Application>
 
27:<Uses-SDK Android: minsdkversion= "3" />
 
28:<! --
 
29: The two activities configured above have the same action type, all of which are "android. Intent. Action. Edit"
 
30: When the action attribute of intent is intent. action_edit, the system does not know which activity to switch,
 
31: A dialog will pop up listing all actions as "android. Intent. Action. Edit"
 
32: Activity for users
 
33: -->
34: </Manifest> 

Ii. Intent Constructor

Common constructor:

1. Intent () null Constructor

2. Intent (intent O) copy constructor

3. Intent (string action) specifies the constructor of the action type.

4. Intent (string action, Uri URI) specifies the action type and Uri constructor. Uri is mainly used in combination with data sharing between programs. contentprovider

5. Intent (context packagecontext, class <?> CLs) input component constructor, that is, the aforementioned

6. Intent (string action, Uri, context packagecontext, class <?> CLs) the first two combinations

Intent has six constructor types. 3, 4, and 5 are the most commonly used and are not useless!

The action of Intent (string action, Uri URI) is the name attribute value corresponding to the Action node in androidmainfest. xml. Many action and category constants are defined in the intent class.

Sample Code 2:

 
1:Intent intent = new intent (intent. action_edit, null );
2:Startactivity (intent );

The second example uses the fourth constructor, but the URI parameter is null. When this code is executed, the system will find it in the main configuration file androidmainfest. xml.

<Action Android: Name = "android. intent. action. edit "/> corresponding activity. If multiple activities have <action Android: Name =" android. intent. action. edit "/> A dailog selection activity is displayed, for example:

This is not the case if the sample code is used for sending.

3. Use intent to transmit data between activities

Run the following code in main:

 
1:Bundle bundle =NewBundle ();
 
2:Bundle. putstringarray ("Namearr", Namearr );
 
3:Intent intent =NewIntent (main.This, Countlist.Class);
 
4:Intent. putextras (bundle );
 
5:Startactivity (intent );

In countlist, the Code is as follows:

 
1:Bundle bundle =This. Getintent (). getextras ();
 
2:String [] arrname = bundle. getstringarray ("Namearr");

The above code implements data transmission between activities!

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.