Request Code and result code
Request Code and result code 1. Introduction
Request Code:
For example, if the request page has multiple buttons, you can find the button in the request according to the request code.
Result code:
Multiple requests can open multiple pages. Based on the result code, we can see which interface we are opening.
The request code is used to identify the request source, and the result code is used to identify the result source.
Ii. Steps
The result code is demonstrated here.
1. The result code in interface 1 is 100
SetResult (100, intent );
2. The result code in interface 2 is 200
SetResult (200, intent );
3. In the onActivityResult method on the main interface, determine the page from which the data comes from based on the result code.
Switch (resultCode ){
Case 100:
Et_phoneNumber.setText (bundle. getString ("phoneNumber "));
Break;
Case 200:
Et_phoneNumber2.setText (bundle. getString ("phoneNumber2 "));
Break;
Default:
Break;
}
Iii. code example
The returned values in the following code are essentially the same as those transmitted using intent. They are getExtras and putExtras.
Here is the test result code. The test request code is included in the code comment.
Result chart:
Open the first button to enter an interface. Open the second button to enter another interface.
Code:
ActivityReturnData. MainActivity
1 package activityReturnData; 2 3 4 5 6 import com. example. activityReturnData. r; 7 8 import android. app. activity; 9 import android. content. intent; 10 import android. OS. bundle; 11 import android. view. view; 12 import android. view. view. onClickListener; 13 import android. widget. button; 14 import android. widget. editText; 15 16 17 18 public class MainActivity extends Activity {19 private Button btn_chooseContacter; // create a button object 20 private EditText et_phoneNumber; 21 private Button btn_chooseContacter2; // create a button object 22 private EditText et_phoneNumber2; 23 protected void onCreate (Bundle savedInstanceState) {24 super. onCreate (savedInstanceState); // The parent class operation 25 setContentView (R. layout. activity_main); // introduce interface 26 btn_chooseContacter = (Button) findViewById (R. id. btn_chooseContacter); // find the button27 et_phoneNumber = (EditText) findViewById (R. id. et_phoneNum); 28 btn_chooseContacter.setOnClickListener (new OnClickListener () {// set the button to listen to 29 30 @ Override31 public void onClick (View v) {// onclick event 32 // TODO Auto-generated method stub33 Intent intent = new Intent (MainActivity. this, Activity01.class); // initialize intent34 // Request Code: resultCode35 startActivityForResult (intent, 1); 36} 37}); 38 39 40 btn_chooseContacter2 = (Button) findViewById (R. id. btn_chooseContacter2); // find the button41 et_phoneNumber2 = (EditText) findViewById (R. id. et_phoneNum2); 42 btn_chooseContacter2.setOnClickListener (new OnClickListener () {// set the button to listen for 43 44 @ Override45 public void onClick (View v) {// onclick event 46 // TODO Auto-generated method stub47 Intent intent = new Intent (MainActivity. this, Activity02.class); // initialize intent48 // Request Code: resultCode49 startActivityForResult (intent, 2); 50} 51 }); 52 53} 54/* 55 * Activity01 after the finish () method is called, the onActivityResult method of MainActivity is called 56 * because the method 57 * (non-Javadoc) 58 * @ see android will be returned after Activity01 is completed. app. activity # onActivityResult (int, int, android. content. intent) 59 */60 @ Override61 protected void onActivityResult (int requestCode, int resultCode, Intent data) {62 // TODO Auto-generated method stub63 super. onActivityResult (requestCode, resultCode, data); 64 // if no value is returned, directly return is OK 65 if (data = null) return; 66 Bundle bundle = data.GetExtras(); 67 String phoneNumber = bundle. getString ("phoneNumber"); 68 69 // switch (requestCode) {70 // case 1: 71 // et_phoneNumber.setText (phoneNumber); 72 // break; 73 // case 2: 74 // et_phoneNumber2.setText (phoneNumber); 75 // break; 76 // default: 77 // break; 78 //} 79 80 switch (resultCode) {81Case 100:82 et_phoneNumber.setText (bundle. getString ("phoneNumber"); 83 break; 84Case 200:85 et_phoneNumber2.setText (bundle. getString ("phoneNumber2"); 86 break; 87 default: 88 break; 89} 90 91} 92}
ActivityReturnData. Activity01
1 package activityReturnData; 2 3 4 import com. example. activityReturnData. r; 5 6 import android. app. activity; 7 import android. content. intent; 8 import android. OS. bundle; 9 import android. view. view; 10 import android. view. view. onClickListener; 11 import android. widget. textView; 12 13 public class Activity01 extends Activity {14 private TextView TV _phoneNum; 15 @ Override16 protected void onCreate (Bundle savedInstanceState) {17 // TODO Auto-generated method stub18 super. onCreate (savedInstanceState); 19 setContentView (R. layout. activity01); 20 TV _phoneNum = (TextView) findViewById (R. id. TV _phoneNum); 21 TV _phoneNum.setOnClickListener (new OnClickListener () {22 23 @ Override24 public void onClick (View v) {25 // TODO Auto-generated method stub26 Intent intent = new Intent (); 27 intent.PutExtra("PhoneNumber", TV _phoneNum.getText (); 28 // resultCode, result code 29SetResult (100, intent );30 finish (); 31} 32}); 33} 34}
ActivityReturnData. Activity02
1 package activityReturnData; 2 3 4 import com. example. activityReturnData. r; 5 6 import android. app. activity; 7 import android. content. intent; 8 import android. OS. bundle; 9 import android. view. view; 10 import android. view. view. onClickListener; 11 import android. widget. textView; 12 13 public class Activity02 extends Activity {14 private TextView TV _phoneNum2; 15 @ Override16 protected void onCreate (Bundle savedInstanceState) {17 // TODO Auto-generated method stub18 super. onCreate (savedInstanceState); 19 setContentView (R. layout. activity02); 20 TV _phoneNum2 = (TextView) findViewById (R. id. TV _phoneNum2); 21 TV _phoneNum2.setOnClickListener (new OnClickListener () {22 23 @ Override24 public void onClick (View v) {25 // TODO Auto-generated method stub26 Intent intent = new Intent (); 27 intent.PutExtra("PhoneNumber2", TV _phoneNum2.getText (); 28 // resultCode, result code 29SetResult (200, intent );30 finish (); 31} 32}); 33} 34}
/ActivityReturnData/AndroidManifest. xml
1 <manifest xmlns:android="http://schemas.android.com/apk/res/android" 2 package="com.example.activityReturnData" 3 android:versionCode="1" 4 android:versionName="1.0" > 5 6 <uses-sdk 7 android:minSdkVersion="8" 8 android:targetSdkVersion="19" /> 9 10 <application11 android:allowBackup="true"12 android:icon="@drawable/ic_launcher"13 android:label="@string/app_name"14 android:theme="@style/AppTheme" >15 <activity16 android:name="activityReturnData.MainActivity"17 android:label="@string/app_name" >18 <intent-filter>19 <action android:name="android.intent.action.MAIN" />20 21 <category android:name="android.intent.category.LAUNCHER" />22 </intent-filter>23 </activity>24 <activity android:name="activityReturnData.Activity01" android:exported="true"></activity>25 <activity android:name="activityReturnData.Activity02" android:exported="true"></activity>26 </application>27 28 </manifest>
/ActivityReturnData/res/Layout/Activity_main.xml
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:orientation="vertical" > 6 7 <EditText 8 android:id="@+id/et_phoneNum" 9 android:layout_width="match_parent"10 android:layout_height="wrap_content"11 android:ems="10" >12 13 <requestFocus />14 </EditText>15 16 <Button17 android:id="@+id/btn_chooseContacter"18 android:layout_width="318dp"19 android:layout_height="50dp"20 android:text="@string/btn_chooseContacter" />21 22 <EditText23 android:id="@+id/et_phoneNum2"24 android:layout_width="match_parent"25 android:layout_height="wrap_content"26 android:ems="10" >27 28 <requestFocus />29 </EditText>30 31 <Button32 android:id="@+id/btn_chooseContacter2"33 android:layout_width="318dp"34 android:layout_height="50dp"35 android:text="@string/btn_chooseContacter" />36 37 </LinearLayout>