Android Intent (Intent) and androidintent Intent

Source: Internet
Author: User

Android Intent (Intent) and androidintent Intent

Intent is an important bridge in Android. It can be divided into explicit Intent and implicit Intent. Next we will explain the two intentions respectively.

  • Explicit intent: activates the internal activity of the application 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 to; the second parameter refers to the Activity to jump to startActivity (intent ); // if no callback is available, OK or Intent intent = new Intent (); intent. setClassName ("com. sd. study. test_01 "," com. sd. study. test_02 "); // The first parameter indicates the package name of the Activity to jump to; the second parameter indicates the package name startActivity (intent) of the Activity to jump ); or Intent intent = new Intent (this, Main2Activity. class); // The first parameter refers to the Activity to jump to; the second parameter refers to the Activity to jump to startActivity (intent );

  

  • Implicit intent: Open the Activity of another program by specifying the package name and class name.
// Implement the jump function Intent intent = new Intent (); // sets the jump action intent. setAction ("name of the target activity in the list file"); // you can specify categoryintent. addCategory ("category of the target activity in the list file"); // enable the Activity (add the permission if needed) startActivity (intent ); // call Intent intent = new Intent (); // sets the call Action intent. setAction ("name of the target activity in the list file"); // sets the intent of the dial data. setData (Uri. parse ("tel:" + 119); // enable Activity (if you need to add permissions, remember to add) startActivity (intent );

  

// Configuration 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 "> //" name of the target activity in the list file "//" category of the target activity in the list file "</activity> </application>

  • Android data transmission and return
// The first Activitypublic class MainActivity extends Activity {private Button btn; protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); btn = (Button) findViewById (R. id. button); // set the Click Event btn. setOnClickListener (new OnClickListener () {@ Override public void onClick (View v) {Intent intent = new Intent (); intent. setClass (mainActivi Ty. this, main2Activity. class); // The first parameter refers to the Activity to jump to; the second parameter refers to the Activity intent to jump. putExtra ("str1", "hello world"); intent. putExtra ("str2", "Hello, world"); // if there is no callback, you do not need to rewrite this method startActivityForResult (intent, 1 );}});}} // The second Activitypublic class Main2Activity extends Activity {protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); B Tn = (Button) findViewById (R. id. button); Intent intent = this. getIntent (); Bundle bundle = intent. getExtras (); // value String str1 = bundle based on "key-value pairs. getString ("str1"); String str2 = bundle. getString ("str2"); // click the btn button. setOnClickListener (new OnClickListener () {@ Override public void onClick (View v) {Intent intent = new Intent (); intent. putExtra ("str3", "Back Data"); setResult (0, intent); // set the returned value fin Ish (); // call this method. This Activity is closed and removed from the stack. Then enter the previous Activity }});}}
    • Data Transmission
      • PutExtra (); // You can transmit eight basic data types;
      • Bundle (); // You can transmit eight basic data types;
    • Return data
      • A. Select startActivityForResult () for the new interface of the caller Activity ();
StartActivityForResult (intent, 1); // if there is no callback, you do not need to override this method.
      • B. Set the return value in the newly opened Activity.
Intent intent = new Intent (); intent. putExtras (); setResult (); // set the return value
      • C. close finish ();
      • D. Call onActivityResult (); Method in the caller Activity.

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.