/*************************************** **************************************** *************
* Author: conowen @ Dazhong
* E-mail: conowen@hotmail.com
* Http://blog.csdn.net/conowen
* Note: This article is original and only used for learning and communication. For more information, indicate the author and its source.
**************************************** **************************************** ************/
1. Two methods to achieve activity jump
There are two main methods to achieve activity jump: startactivity () and startactivityforresult ();
For example, activity a jumps to Activity B.
For example, the first type is to transmit the data of Activity A to Activity B through intent, but the data cannot be returned.
For the first type, refer to my previous blog post.
Http://blog.csdn.net/conowen/article/details/7270393
2. detailed usage of startactivityforresult () in method 2
This evening we will talk about the second method: startactivityforresult (); you can transfer activity a data to Activity B, or you can return Activity B data to activity A for processing.
public void startActivityForResult (Intent intent, int requestCode)
Startactivityforresult has two parameters. The first parameter is the intent of the current activity (assuming activity a). For the usage of the second parameter, let's take a look at the explanation in the official SDK documentation.
requestCode : If >= 0, this code will be returned in onActivityResult() when the activity exits.
Generally, it is set to be greater than or equal to 0, because activity a may not only jump to Activity B, but also jump to Activity C, D, E ......, All the data returned by these activities are processed by the activity. Then, what can be used by the activity to determine which activity is returned.
Therefore, when activity a jumps to an activity, it is necessary to set the requestcode of the target activity. This requestcode uniquely identifies the corresponding activity.
As shown below, when you want to jump to Activity B, 15 indicates activity B's identifier,
IntentActivity.this.startActivityForResult(intent, 15);
Similarly, when another listener of Activity A needs to jump to Activity C, set Activity C to 25. (It can be greater than or equal to 0)
IntentActivity.this.startActivityForResult(intent, 25);
(The onactivityresult method can be used to process the requestcode values representing different activities. The detailed usage is .)
3. onactivityresult ()
Then activity a processes the data returned by these activities through the onactivityresult method. Onactivityresult is defined as follows:
protected void onActivityResult (int requestCode, int resultCode, Intent data)
3.1 The data processing method onactivityresult () has three parameters. The first requestcode is used to identify the data returned by the activity and can be filtered using the swith statement.
The basic framework code is as follows:
switch (requestCode) {case 15:switch (resultCode) {case 0://do somethingbreak;case 1://do something break;default:break;}break;case 25:switch (resultCode) {case 0: //do somethingbreak;default:break;} default: break; }
3.2. The second parameter is resultcode, which is also an int type. If Activity B has several different returned results, it can also be filtered by resultcode.
When you jump to another activity, such as activity B. You need to know that the processing method for returning Activity B data to activity A is setresult (INT resultcode, intent data). The first parameter is resultcode, that is, corresponding to the corresponding processing method. The recommended values are as follows. These are macro variables and the essence is int. Of course, you can also define it as a numerical value.
RESULT_CANCELEDRESULT_OKRESULT_FIRST_USER
The specific method is as follows: for example, you can make the following settings in Activity B. The two button listeners correspond to two resultcodes, which correspond to two processing methods. As for what you want to do, define the listener.
button1.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stub//do somethingsecondactivity.this.setResult(0, intent);secondactivity.this.finish();}});button2.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stub//do somethingsecondactivity.this.setResult(1, intent);secondactivity.this.finish();}});
3.3. The third parameter is intent data, which is the value returned by the activity, for example, data. getstringextra ("key.
Note: When you call the setresult () method to jump back to the original activity, you must call the finish method to end the current activity.
In addition, if activity a only needs to jump to Activity B, the request code can only be greater than or equal to 0 during the jump, and the data processing method onactivityresult () is structured as follows, you do not need to filter the request code.
switch (resultCode) {case 0://do somethingbreak;case 1://do something break;default:break;}
========================================================== ========================================================== ====================
Attached detailed code
Jump from the first activity to the second activity, mainly to verify the resultcode,
Jump from the first activity to the third activity and to the second activity, mainly to verify requestcode
:
// Intentactivity is the first activitypackage conowen. activity. intent; 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; import android. widget. textview; import android. widget. toast; public class intentactivity extends activity {/** called when the activity is first created. * // @ overridepublic void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); button bt = (button) findviewbyid (R. id. BT); button bt2 = (button) findviewbyid (R. id. bt13); BT. setonclicklistener (new view. onclicklistener () {@ overridepublic void onclick (view v) {// todo auto-generated method stubintent intent = new intent (intentactivity. this, secondactivity. class); // bind the intent to the second activity, and then start to jump to the second activityintentactivity. this. startactivityforresult (intent, 15); // The second parameter is requestcode, greater than or equal to 0,}); bt2.setonclicklistener (New onclicklistener () {@ overridepublic void onclick (view V) {// todo auto-generated method stubintent intent = new intent (intentactivity. this, requestcodetest. class); // bind the intent to the third activityintentactivity. this. startactivityforresult (intent, 25) ;}}) ;}@ overrideprotected void onactivityresult (INT requestcode, int resultcode, intent data) {// todo auto-generated method stubsuper. onactivityresult (requestcode, resultcode, data); Switch (requestcode) {// use requestcode to identify the data from which activitycase 15: // obtain data from activity2, and displayed on the screen. Switch (resultcode) {// use resultcode to identify the corresponding processing method of the activity. Case 0: String resultstr1 = data. getstringextra ("result"); // retrieve the passed data textview TV = (textview) findviewbyid (R. id. TV); TV. settext (resultstr1); // display the break; Case 1: String resultstr2 = data. getstringextra ("result"); textview TV1 = (textview) findviewbyid (R. id. TV); tv1.settext (resultstr2); toast. maketext (this, "The requestcode value of 2nd activities is" + requestcode, toast. length_long ). show (); break; default: break;} break; Case 25: Switch (resultcode) {Case result_ OK: String resultstr3 = data. getstringextra ("result3"); toast. maketext (this, "The requestcode value of 3rd activities is" + requestcode, toast. length_long ). show (); textview TV = (textview) findviewbyid (R. id. TV); TV. settext (resultstr3); break; default: break ;}}}
// Secondactivity is the second activitypackage conowen. activity. intent; 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; import android. widget. edittext; public class secondactivity extends activity {@ overrideprotected void oncreate (bundle savedinstancestate) {// todo auto-generated method stubsuper. oncreate (savedinstancestate); setcontentview (R. layout. secondactivity); // obtain the intentbutton returnbt1 = (button) findviewbyid (R. id. returnbt1); Final intent = getintent (); listener (New onclicklistener () {@ overridepublic void onclick (view v) {// todo auto-generated method stubedittext ET = (edittext) findviewbyid (R. id. et); string res = et. gettext (). tostring (); intent. putextra ("result", Res); // insert data into the intent secondactivity. this. setresult (0, intent); // jump back to the original activitysecondactivity. this. finish (); // be sure to end the current activity}); button returnbt2 = (button) findviewbyid (R. id. returnbt2); returnbt2.setonclicklistener (New onclicklistener () {@ overridepublic void onclick (view v) {// todo auto-generated method stubintent. putextra ("result", "verify result Code"); secondactivity. this. setresult (1, intent); secondactivity. this. finish ();}});}}
// Requestcodetest is the third activitypackage conowen. activity. intent; 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; import android. widget. edittext; public class requestcodetest extends activity {@ overrideprotected void oncreate (bundle savedinstancestate) {// todo auto-generated method stubsuper. oncreate (savedinstancestate); setcontentview (R. layout. requestcode); // The XML file name of layout cannot be an uppercase letter: button bt3 = (button) findviewbyid (R. id. returnbt3); bt3.setonclicklistener (New onclicklistener () {@ overridepublic void onclick (view V) {// todo auto-generated method stub // This activity is mainly used to verify requestcodeedittext ET3 = (edittext) findviewbyid (R. id. ET3); intent = getintent (); // obtain the intentstring res = et3.gettext () that starts the activity (). tostring (); intent. putextra ("result3", Res); requestcodetest. this. setresult (result_ OK, intent); requestcodetest. this. finish ();}});}}