The messenger between Android components--intent

Source: Internet
Author: User

Booting from one activity to another activity can use the StartActivity () method or the Startactivityforresult () method

First: Start an activity directly
Intent Intent = new Intent (main.this, Secondactivity.class);
StartActivity (Intent);

Second type: Start another activity and return the result
Function: When jumping from the second activity to the previous activity, it is no longer necessary to use the startactivity, that is, not to use the StartActivity method two times
Startactivityforresult (Intent Intent, Int requestcode)
Intent data and actions to the activity to jump

Requestcode >=0 is good, casually used in Onactivityresult () to distinguish which sub-module callback data, if there are C.java, D or even e sub-modules, each district separate different requestcode is good.

Java code
  1. Public class Mainactivity extends Activity {
  2. /** Called when the activity is first created. * /
  3. @Override
  4. public void OnCreate (Bundle savedinstancestate) {
  5. super.oncreate (savedinstancestate);
  6. Setcontentview (R.layout.main);
  7. //Add the bound event to the button
  8. Button MyButton = (button) Findviewbyid (R.id.mybutton);
  9. Mybutton.settext ("My first button");
  10. Mybutton.setonclicklistener (new OnClick ());
  11. }
  12. @Override
  13. protected void Onactivityresult (int requestcode, int resultcode, Intent data) {
  14. System.out.println (Requestcode);
  15. System.out.println (ResultCode);
  16. }
  17. public class OnClick implements onclicklistener{
  18. @Override
  19. public void OnClick (View v) {
  20. //Generate a Intent object
  21. Intent Intent = new Intent ();
  22. Intent.putextra ("testintent", "Testextra");
  23. Intent.setclass (mainactivity. This,secondactivity.  class);
  24. //Start an activity directly
  25. StartActivity (Intent);
  26. //Start an activity with a return value
  27. Startactivityforresult (Intent, 2);
  28. }
  29. }
  30. }


Setresut (int resultcode, Intent Intent)
ResultCode If the activity sub-module of the jump may have several different results returned, it can be distinguished by this parameter. There is also a special RESULT_OK value, there is no special case to use it, the SDK is described.
Intent continued not to explain, passed back to a of the Onactivityresult ()


Onactivityresult (int requestcode, int resultcode, Intent Intent)
Here are three not to explain, and the above corresponding to the thing. If you do not distinguish between Requestcode and ResultCode, as long as there are other activity setresult to a onactivityresult () will be treated without discrimination.

Java code
  1. Public class Secondactivity extends activity{
  2. @Override
  3. protected void OnCreate (Bundle savedinstancestate) {
  4. //TODO auto-generated method stub
  5. super.oncreate (savedinstancestate);
  6. This.setcontentview (R.layout.other);
  7. Button btn = (button) Findviewbyid (R.id.otherbutton);
  8. Btn.setonclicklistener (new Button.onclicklistener () {
  9. @Override
  10. public void OnClick (View v) {
  11. Intent Intent = Getintent ();
  12. Setresult (3, intent);
  13. Finish ();
  14. }
  15. });
  16. }
  17. }

How do I use the Custom Action property?
1. Define a custom action name--constant

Java code
    1. Public static final String my_action = "Hb.com.MY_ACTION";


2. Use a button and then bind the event to it and let it jump to another activity

Java code
  1. Myactionbtn.setonclicklistener (new Button.onclicklistener () {
  2. @Override
  3. public void OnClick (View v) {
  4. System.out.println ("myactionbtn");
  5. Intent Intent = Getintent ();
  6. This must be a new intent object, if the current activity is opened with the above
  7. Intent Intent = new Intent ();
  8. Intent.setaction (my_action);
  9. StartActivity (Intent);
  10. }
  11. });

3. Add the name of the activity in the Androidmanifest.xml configuration file

XML code
  1. <activity android:name=". Secondactivity " android:label=" @string/secondactivity ">
  2. <intent-filter>
  3. <action android:name="Hb.com.MY_ACTION" />
  4. <category android:name="Android.intent.category.DEFAULT" />
  5. </intent-filter>
  6. </activity>

Note: Hb.com.MY_ACTION This value is the same as intent.setaction (my_action);
Activity tag inside the application tag
Category This property must appear in the intent filter, otherwise it cannot be tested by

The data property of the intent is the URI and MIME type of the specified action, and different actions have different data designations
The category attribute in intent is an additional information for executing an action
The Extras property of intent is additional information for adding some components

Java code
  1. Show contact information for _id 1
  2. data = "CONTENT://CONTACTS/PEOPLE/1";
  3. URI = uri.parse (data);
  4. Action_view Displaying data to users
  5. Intent.setaction (Intent.action_view);
  6. Intent.setdata (URI);
  7. StartActivity (Intent);
  8. Edit contact information for _id 1
  9. data = "CONTENT://CONTACTS/PEOPLE/1";
  10. URI = uri.parse (data);
  11. Action_edit Display editable data
  12. Intent.setaction (Intent.action_edit);
  13. Intent.setdata (URI);
  14. StartActivity (Intent);
  15. Display the phone call interface
  16. data = "tel:13811111111";
  17. URI = uri.parse (data);
  18. Action_dial display the panel that dials the phone
  19. Intent.setaction (intent.action_dial);
  20. Intent.setdata (URI);
  21. StartActivity (Intent);
  22. Direct phone calls
  23. data = "tel:13811111111";
  24. URI = uri.parse (data);
  25. Action_call Call directly
  26. Intent.setaction (Intent.action_call);
  27. Intent.setdata (URI);
  28. StartActivity (Intent);
  29. accessing the browser
  30. data = "http://www.baidu.com";
  31. URI = uri.parse (data);
  32. Action_view Displaying data to users
  33. Intent.setaction (Intent.action_view);
  34. Intent.setdata (URI);
  35. StartActivity (Intent);
  36. Access Map
  37. data = "geo://39.92,116.46";
  38. URI = uri.parse (data);
  39. Action_view Displaying data to users
  40. Intent = New Intent (Intent.action_view,uri);
  41. StartActivity (Intent);

The action in the <intent-filter> list cannot be empty, or the program block cannot pass.
If the intent object specifies an action property, then to pass the action test, the property specified by the intent object must match the intent filter, otherwise it cannot pass the test.
If the Action property is not specified in the intent object, the test is passed automatically.

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.