Activity -- jump and pass value

Source: Internet
Author: User

To support the original: http://blog.csdn.net/avatarliyu/article/details/17490211
JumpActivity jump refers to the jump from one Activity to another. There are two common implementation methods:1. startActivity (Intent)2. startActivityForResult (Intent, int)
Before introducing these two methods, we should first introduce Intent, which, as its name implies, is "Intent", which abstracts the execution of an operation. It can start an Activity or a Service. intent can not only connect components within the program, but also directly transmit information in the program. It will be introduced later.
StartActivity (Intent)This method can directly jump to another activity or pass a value to another Activity. The direct jump code is as follows:

Intent intent = new Intent(ActivityA.this, ActivityB.class);startActivity(intent);


I shamelessly used the previous Code. This is a jump from ActivityA to ActivityB. Next we will talk about the second method of jump first.
StartActivityForResult (Intent, int)
When we expect that the Activity after the jump can return results for me, the main method used is : SetResut (int resultCode, Intent intent)
OnActivityResult (int requestCode, int resultCode, Intent intent)

Next I will use an example to illustrate that I don't like boring programming. We assume ActivityA is A boy, ActivityB is A girl, and boys A go to the library to see an empty seat opposite B, boys ask girls if they can sit there. You need to know how the code is:
ActivityA:
Public class ActivityA extends Activity {private Context context = this; // called during Activity creation @ Override public void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_a); Button btnB = (Button) findViewById (R. id. button_ B); btnB. setOnClickListener (new View. onClickListener () {@ Override public void onClick (View v) {Intent intent = new Intent (cont Ext, ActivityB. class); intent. putExtra ("boy", ""); startActivityForResult (intent, 0); // here 0 is a basis, you can write other values, but be sure to >=0 }});} protected void onActivityResult (int requestCode, int resultCode, Intent data) {switch (resultCode) {// resultCode indicates the flag of the callback, RESULT_ OK case 1: Toast. makeText (context, "yes. ", Toast. LENGTH_SHORT). show (); break; case 2: Toast. makeText (context," Sorry, this is someone. ", Toast. LENGTH_SHORT). show (); break; default: break ;}}}


ActivityB:
Public class ActivityB extends Activity {private static int RESULT_ OK = 0; private Intent intent; // called at Activity creation @ Override public void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_ B); intent = getIntent (); String question = intent. getStringExtra ("boy"); if (question. equals ("Gao Shuai Fu") {RESULT_ OK = 1;} else {RESULT_ OK = 2;} Button btnA = (Button) findViewById (R. id. button_a); btnA. setOnClickListener (new View. onClickListener () {@ Override public void onClick (View v) {setResult (RESULT_ OK, intent); // intent is the intent with Bundle sent from, of course, you can also define a new Bundle finish ();}});}}

I won't say much about the running results. Everyone understands them. Let's talk about the procedure.1. In ActivityA, start ActivityB by calling startActivityForResult (intent, 0) and pass the value in intent to ActivityB. 2. ActivityB obtains the value passed by ActivityA through intent = getIntent (), and then judges the return result.3. ActivityB sets the returned result by calling setResult (int resultCode, intent.4. Note: The finish () method in ActivityB must be called. Otherwise, ActivityA cannot be returned.5. ActivityA calls onActivityResult (int requestCode, int resultCode, Intent intent) to get the result returned by ActivityB. Generally, resultCode is used to indicate the result and intent is used to pass the value.
It is easy to pass values. intent. putExtra (key, value) can be used. The key must use String to indicate the name of the value you pass, and the basic data type of the value can be set. You can also encapsulate the passed content into the Bundle and use the intent. putExtra (String name, Bundle bundle) method to pass the value.
Let's use the example above. I am a lazy ....
Public class ActivityA extends Activity {private Context context = this; // called during Activity creation @ Override public void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_a); Button btnB = (Button) findViewById (R. id. button_ B); btnB. setOnClickListener (new View. onClickListener () {@ Override public void onClick (View v) {Intent intent = new Intent (cont Ext, ActivityB. class); Bundle bundle = new Bundle (); bundle. putString ("name", "boys"); bundle. putDouble ("high", 1.65); intent. putExtra ("appearance", bundle); startActivityForResult (intent, 0); // here 0 is a basis, you can write other values, but be sure to >=0 }});} protected void onActivityResult (int requestCode, int resultCode, Intent data) {switch (resultCode) {// resultCode indicates the flag of the callback, RESULT_ OK case 1: Toast. makeText (context, "yes. ", Toast. LENGTH_SHORT). show (); break; case 2: Toast. makeText (context," Sorry, this is someone. ", Toast. LENGTH_SHORT). show (); break; default: break ;}}}


Public class ActivityB extends Activity {private static int RESULT_ OK = 0; private Intent intent; // called at Activity creation @ Override public void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_ B); Bundle bundle = getIntent (). getBundleExtra ("appearance"); double high = bundle. getDouble ("high"); intent = getIntent (); if (high <1.8) {RESULT_ OK = 2;} else {RESULT_ OK = 1;} Button btnA = (Button) findViewById (R. id. button_a); btnA. setOnClickListener (new View. onClickListener () {@ Override public void onClick (View v) {setResult (RESULT_ OK, intent); // intent is the intent with Bundle sent from, of course, you can also define a new Bundle finish ();}});}}


Now, we will introduce the jump and value transfer here first. The source code of the above example is here. We will introduce how to use the Parcelable interface to pass custom class objects.












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.