First, what is intent?
The Chinese meaning of intent is the purpose. It also means "purpose" in Android. Is where we're going, from the activity to the other activity needs to use the intent.
Example code one:
1://define a intent
2:intent Intent = new Intent (intentdemo.this, Anotheractivity2.class);
3://Start activity
4:startactivity (Intent);
The above example code is a function of switching from Intentdemo activity to AnotherActivity2. This is intent one of the construction methods, specifying two activity. Why do you need to specify two activities? Because there is an active stack in Android, the way to make sure that the previous activity is pushed into the stack correctly, the activity can correctly stack when the return key is triggered.
Note: All activity must first configure the declaration in Androidmanifest.xml. The program configuration file used for this article
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"/>
Ten: </intent-filter>
One: </activity>
: <activity android:name= ". Anotheractivity "android:label=" another ">
: <intent-filter>
: <action android:name= "Android.intent.action.EDIT"/>
: <!--category must be configured, otherwise error: Cannot find activity--
: <category android:name= "Android.intent.category.DEFAULT"/>
: </intent-filter>
: </activity>
19:
: <activity android:name= ". AnotherActivity2 "android:label=" Another2 ">
: <intent-filter>
A: <action android:name= "Android.intent.action.EDIT"/>
Page: <category android:name= "Android.intent.category.DEFAULT"/>
: </intent-filter>
: </activity>
: </application>
: <uses-sdk android:minsdkversion= "3"/>
: <!--
29: The two activity configured above has the same action type, both "Android.intent.action.EDIT"
30: When the intent ACTION property is Intent.action_edit, the system does not know which activity to turn to,
31: A dialog will pop up to list all the action "Android.intent.action.EDIT"
32:activity for users to choose
:-
: </manifest>
Second, the intent of the structure function
Public constructors:
1. Intent () empty 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 constructor of the action type and URI, and the URI is primarily a data share between the programs ContentProvider
5, Intent (Context packagecontext, class<?> CLS) The constructor of the incoming component, which is referred to above
6. Intent (String action, Uri Uri, Context packagecontext, class<?> CLS) The first two binding bodies
Intent has six kinds of constructors, 3, 4, 5 are the most commonly used, not other useless!
The action of the Intent (String action, Uri URI) is the Name property value corresponding to the action node in Androidmainfest.xml. A number of action and category constants are defined in the intent class.
Example code two:
1:intent Intent = new Intent (intent.action_edit, NULL);
2:startactivity (Intent);
Example code two uses a fourth constructor, except that the URI parameter is null. When executing this code, the system will look in the program master configuration file Androidmainfest.xml
<action android:name= "Android.intent.action.EDIT"/> corresponding activity, if corresponding to multiple activity has <action android:name= " Android.intent.action.EDIT "/> will pop up a Dailog select activity at this point, such as:
This is not the case if you are sending in sample code one way.
Third, using intent to transfer data between activity
In Main, execute the following code:
1:bundle bundle = new Bundle ();
2:bundle.putstringarray ("Namearr", Namearr);
3:intent Intent = new Intent (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 realizes the data transfer between the activity!
Use of destination intent in Android