The concept of intent
Intent's official explanation is that "an was Intent a messaging object can use the to request an action from the another app component. "The app component here refers to the four components of Android Activity,service,contentprovider,broadcastreceiver. Different intent enable these components to produce corresponding actions, providing interactivity between these components. Then the specific use of this "messaging object" has "start an activity", "Start a service", "provide a broadcast".
Create and register a new activity
First, you write a class. The class needs to inherit from the activity. Depending on the activity's life cycle, the OnCreate method is typically overridden to complete the initialization of the activity subclass, and the activity layout must be defined with Setcontentview () to initialize the activity. Creating the Secondactivity Class
public class Secondactivity extends Activity {@Overrideprotected void onCreate (Bundle savedinstancestate) { Super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_second);}}
and define a layout file for it
<?XML version= "1.0" encoding= "Utf-8"?><LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"android:orientation= "vertical" > <TextViewAndroid:layout_width= "Match_parent"Android:layout_height= "Match_parent"Android:text= "@string/second_activity"/></LinearLayout>
This string/second_activity defines the text that is displayed.
The second step is to register the activity sub-class. The registration file is located in the/project name/androidmanifest.xml file, modifies the file, and registers the activity subclass that was written. Note Four mentions that Androidmanifest.xml is "an Android project configuration file." The various components used in the Android project need to be registered in this file, so the newly added activity will be registered in Androidmanifest.xml. In fact, each component that the user uses must be defined within the <application> node in the Androidmanifest.xml file. The modified androidmanifest.xml are as follows:
<?XML version= "1.0" encoding= "Utf-8"?><Manifestxmlns:android= "Http://schemas.android.com/apk/res/android" Package= "Com.example.learnerkiwi"Android:versioncode= "1"Android:versionname= "1.0" > <USES-SDKandroid:minsdkversion= "+"android:targetsdkversion= "+" /> <ApplicationAndroid:allowbackup= "true"Android:icon= "@drawable/ic_launcher"Android:label= "@string/app_name"Android:theme= "@style/apptheme" > <ActivityAndroid:name=". Mainactivity "Android:label= "@string/app_name" > <Intent-filter> <ActionAndroid:name= "Android.intent.action.MAIN" /> <categoryAndroid:name= "Android.intent.category.LAUNCHER" /> </Intent-filter> </Activity> <Activity <!--registered second_activity--> here.Android:name= ". Second_activity"Android:label= "@string/second_activity"/> </Application></Manifest>
Implementation of explicit start-up of secondactivity by mainactivity
The general idea is to place a button on the mainactivity and then start secondactivity by clicking the button.
First add the layout of a button component on the Mainactivity.xml
< button android:id = "@+id/start_button" = "Wrap_content" = "Wrap_content" = "@+id/textview1" Android:layout_below = "@+id/textview1" Android:layout_margintop = "20DP" Android:text = "@string/start_button" />
Then modify the OnCreate () method in the Mainactivity.java
protected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); Button Button=(Button) Findviewbyid (R.id.start_button); //set up Click eventsButton.setonclicklistener (NewOnclicklistener () {@Override Public voidOnClick (View v) {//generate Intent, the second parameter is the activity to startIntent Intent =NewIntent (mainactivity. This, second_activity.class); StartActivity (Intent); } });
This implements a Onclicklistener anonymous inner class, which overrides the OnClick () method and constructs a new intent object in the method.
Intent is constructed as Intent (Context Packgecontext, Class <?> CLS), and two construction parameters indicate the class name of the current component and the class name of the target component, respectively. The two component classes are also developed because there is an active stack in Android, which ensures that the activity of the previous component is properly pushed into the stack before the return key is triggered by the previous component activity to correctly stack.
The next sentence of the StartActivity function is the activity class's own method, intent as a parameter. Because the class of the target component is encapsulated in intent, the target component can be started by this method.
Android Learning note 6 components communication and broadcast messages (i)