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