Android first line Learn code note Four---Use intent to shuttle between activities

Source: Internet
Author: User

One: Use display intent

Right-click Com.example.firstactivity Package->new->activity->empty activity, the event name is secondactivity, and tick generate Layout File, Name the layout file secondlayout, but do not tick the Launcher Activity option, click Finish, open the Secondlayout.xml, replace the code inside with the following:

<?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/button2"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:text= "Button2"/></LinearLayout>

Or define a button that shows Button2 on the button, and then the code in the secondactivity is automatically generated as part of the default, which remains the same. Any other activity needs to be registered in Androidmanifest.xml, the system has registered for us, because Secondactivity is not the main activity, so we do not need to configure the contents of the <intent-filter> tag, Now that the second activity has been created, the rest is how to start a second activity---Intent

Intent: An important way to interact with 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 launch events, start services, and send broadcasts.

The intent has multiple overloads for the constructor:

Intent (Content packagecontent,class<?>cls), the first is required to provide a context for initiating activities, the second parameter Class is to specify the target activity to start, through which the constructor can build the Intent "Intention". The activity class provides a startactivity () method, which is dedicated to initiating the activity, which receives a intent parameter, where we can start the target activity by building a good intent incoming startactivity () method.

To modify a click event for a button in firstactivity:

Button.setonclicklistener (New View.onclicklistener () {public      void OnClick (view view) {           Intent intent=new Intent (firstactivity.this,secondactivity.class);           StartActivity (intent);      } });

First constructs a intent, passes in the Firstactivity.this as the context, passes in the Secondactivity.class as the activity target, "the intention" is obvious, The secondactivity activity is opened on the basis of the firstactivity activity, and then the intent is executed through the StartActivity () method.

We can see that we have successfully started secondactivity this activity, using this method to start the activity, Intent "the intention is obvious, therefore becomes the display Intent".

Two: Implicit intent:

It does not clearly indicate which activity we want to start, but instead specifies a series of more abstract actions and category information, which we refer to the system to analyze the intent and help us find the right activity to start. The appropriate activity is to respond to this implicit intent activity by configuring <intent-filter> under the <activity> tab to specify the action and category that the current activity responds to. Open Androidmainfest.xml and add the following code:

  <ActivityAndroid:name= ". Secondactivity">            <Intent-filter>                <ActionAndroid:name= "Com.example.firstactivity.ACTION_START"/>                <categoryAndroid:name= "Android.intent.category.DEFAULT"/>            </Intent-filter>        </Activity>

In the <action> tab we indicate that the current activity can respond to com.example.firstactivity.ACTION_START this action, and the <category> tag contains some additional information, A more precise indication of the intent that the current activity is capable of responding to can also be a category, which responds to intent only if the action and category specified by intent match the contents of the tag.

To modify the Click event of a button in Firstactivity, the code is as follows:

Button.setonclicklistener (New View.onclicklistener () {public            void OnClick (view view) {                Intent intent=new Intent ("Com.example.firstactivity.ACTION_START");                StartActivity (intent);            }        });

We used another constructor for intent, which directly passed in the action string, indicating that we wanted to start an activity that responds to the action of Com.example.firstactivity.ACTION_START as for category, Because Android.intent.category.DEFAULT is a default category, when calling StartActivity, the category is automatically added to intent, and the re-run program and the display intent effect are the same. Description of the action and category we configured in the <activity> tab is in effect.

Each intent can only specify one action, but can specify multiple category, currently our category has only one default category, now add a custom, you can use the intent Addcategory () method. Because you want <action> and <category> to be able to respond at the same time, declare it in Androidmanifest.xml.

<activity android:name= ". secondactivity" >            <intent-filter>                <action android:name= " Com.example.firstactivity.ACTION_START "/>                <category android:name=" Android.intent.category.DEFAULT "/ >                <category android:name= "Com.example.firstactivity.MY_CATEGORY"/>            </intent-filter>        </activity>

  

Android first line Learn code note Four---Use intent to shuttle between activities

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.