Android's intent and activity

Source: Internet
Author: User

Intent can be said to be the soul of Android, the program to jump and transfer data is basically by intent. Intent is very important in Android applications, and understanding intent is helpful for application programming. In the official Android API document, the Intent is defined as: An Intent was an abstract description of a operation to be performed. A intent is an abstract description of the action to be performed (true enough abstraction). There are 3 types of specific:
1, through the StartActivity method to start a new activity.
2, through the broadcast mechanism can send a intent to any interest in this intent broadcastreceiver.
3, through StartService (Intent) or Bindservice (Intent, serviceconnection, int) to interact with the service behind the scenes.

Here's a look at how to start a new activity with the StartActivity method.
The most common use of intent is to connect the various activity in an application, and if we compare activity to a building block, then intent is like glue that sticks different bricks together to form the house we build. In a program, if you want to start an activity, we usually call the StartActivity method and pass the intent as a parameter, as follows:
startactivity (intent);
This intent either specifies an activity, or contains only the information of the selected activity, but the specific activity to start is determined by the system, and the Android system is responsible for selecting the activity that best meets the matching selection criteria.

Example: Intentdemo
Operating effect:


Code Listing:
Androidmanifest.xml
<?xml version= "1.0" encoding= "Utf-8"? ><manifest xmlns:android= "http://schemas.android.com/apk/res/ Android "package=" Com.rainsong.intentdemo "android:versioncode=" 1 "android:versionname=" 1.0 "> <use S-SDK android:minsdkversion= "one" android:targetsdkversion= "/> <application android:label=" @stri Ng/app_name "android:icon=" @drawable/ic_launcher "> <activity android:name=" mainactivity "and Roid:label= "@string/app_name" > <intent-filter> <action android:name= "android.intent. Action. MAIN "/> <category android:name=" Android.intent.category.LAUNCHER "/> </intent-filte r> </activity> <activity android:name= "otheractivity" android:label= "Otheractiv ity "> </activity> </application></manifest>
Layout file: Main.xml
<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android "    android:orientation=" vertical "    android:layout_width=" match_parent "    android:layout_height= "Match_parent"    >    <button        android:id= "@+id/button1"        android:layout_width= "Match_parent"        android:layout_height= "wrap_content"        android:text= "jump to another activity"    /></linearlayout>
Layout file: Other.xml
<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android "    android:orientation=" vertical "    android:layout_width=" match_parent "    android:layout_height= "Match_parent"    ><textview    android:layout_width= "match_parent"    android:layout_height= "WRAP_" Content "    android:text=" otheractivity "    /></linearlayout>
Java source code file: Mainactivity.java
Package Com.rainsong.intentdemo;import Android.app.activity;import Android.content.intent;import android.os.Bundle ; Import Android.view.view;import Android.view.view.onclicklistener;import Android.widget.button;public class Mainactivity extends activity{    onclicklistener listener1 = null;    Button button1;    /** called when the activity is first created. */    @Override public    void OnCreate (Bundle savedinstancestate)    {        super.oncreate (savedinstancestate) ;        Setcontentview (r.layout.main);                Listener1 = new Onclicklistener () {public            void OnClick (View v) {                Intent  intent1= New Intent ( Mainactivity.this, otheractivity.class);                StartActivity (INTENT1);            }        };        Button1 = (Button) Findviewbyid (r.id.button1);        Button1.setonclicklistener (Listener1);    }}

Java source code file: Otheractivity.java

Package Com.rainsong.intentdemo;import Android.app.activity;import Android.os.bundle;public class OtherActivity Extends activity{    /** called when the Activity is first created. *    /@Override public    void OnCreate (Bundle save Dinstancestate)    {        super.oncreate (savedinstancestate);        Setcontentview (R.layout.other);    }}


Android's intent and activity

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.