Android Intent (Intent)

Source: Internet
Author: User

Intent is one of the important bridges in Android, and it is divided into explicit intent and implicit intent. These two intentions are then explained separately.

    • Explicit intent: Activates activity within the app by specifying a set of data or actions (this is safer than implicit intent)
Intent Intent = new Intent ();  Intent.setclass (Mainactivity.this,main2activity.class); The first parameter refers to the activity to jump, and the second refers to the activity  startactivity (intent) that jumps to,  or intent intent if there is no callback. New Intent ();  Intent.setclassname ("com.sd.study.test_01", "com.sd.study.test_02"); The first parameter refers to the package name of the activity to jump, and the second refers to the activity's package name StartActivity (intent) that jumps to, or intent intent = new intent (this, Main2activity.class);//The first parameter refers to the activity to jump, and the second refers to the activity    startactivity (intent) that jumps to;

  

    • Implicit intent: Open the Activity of another program by specifying a specific package name and class name.
The function of the jump intent intent = new intent ();//Set the action of the Jump Intent.setaction ("name of the target activity in the manifest file");//settings Categoryintent.addcategory ("category of the target activity in the manifest file");//Activate activity (if required, add the permission) startactivity (intent);    The ability to make calls intent intent = new intent ();//Set the action intent.setaction ("name of the target activity in the manifest file");//Set the dialed data Intent.setdata ( Uri.parse ("Tel:" +119));//Open Activity (if necessary add the privilege to remember to add) startactivity (intent);

  

Manifest file <application    android:name= ". MyApp "    android:allowbackup=" true "    android:icon=" @mipmap/ic_launcher "    android:label=" @string/app_ Name "    android:supportsrtl=" true "    android:theme=" @style/apptheme ">    <activity android:name=". Mainactivity ">        <intent-filter>            <action android:name=" Android.intent.action.MAIN "/>            <category android:name= "Android.intent.category.LAUNCHER"/>        </intent-filter>    </ activity>    <activity android:name= ". Main2activity ">//" The target activity in the manifest file name "                                             //" category of target activity in the manifest file "    </activity></ Application>

    • Android data transfer and backhaul
//First Activity Public classMainactivityextendsActivity {PrivateButton btn; protected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);          Setcontentview (R.layout.activity_main); BTN=(Button) Findviewbyid (R.id.button); //Set Click eventsBtn.setonclicklistener (NewOnclicklistener () {@Override Public voidOnClick (View v) {Intent Intent=NewIntent (); Intent.setclass (mainactivity. This, Main2activity.class);//The first parameter refers to the activity to jump, and the second one refers to the activity that jumps to .Intent.putextra ("str1", "Hello World"); Intent.putextra ("Str2", "Hello, World"); //If you don't have a callback, you don't have to override this methodStartactivityforresult (Intent, 1);      }          }); }}//a second activity Public classMain2activityextendsActivity {protected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);           Setcontentview (R.layout.activity_main); BTN=(Button) Findviewbyid (R.id.button); Intent Intent= This. Getintent (); Bundle Bundle=Intent.getextras (); //value based on "key value pair"String str1 = bundle.getstring ("str1"); String str2= Bundle.getstring ("str2"); //button's Click eventBtn.setonclicklistener (NewOnclicklistener () {@Override Public voidOnClick (View v) {Intent Intent=NewIntent (); Intent.putextra ("Str3", "Back Data"); Setresult (0, intent);//set the return valueFinish ();//Call this method, this activity closes and removes it from the stack. Then go to the previous activity            }          }); } } 
      • Data transfer
        • PutExtra (); 8 Basic data types can be passed;
        • Bundle (); 8 Basic data types can be passed;
      • Callback Data
        • A. The caller activity opens a new interface to select Startactivityforresult ();
// If you don't have a callback, you don't have to override this method
        • B. Setting the return value in a newly opened activity
New Intent (); Intent.putextras (); Setresult (); // set the return value
        • C. Operation close Finish ();
        • D. Call Onactivityresult () in the caller activity; Method

Android Intent (Intent)

Related Article

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.