Interaction between two activities: startActivityForResult, startactivityresult
The Code is as follows:
1 package com. zzw. teststartintentforrequest; 2 3 import android. app. activity; 4 import android. content. intent; 5 import android. OS. bundle; 6 import android. util. log; 7 import android. view. view; 8 import android. view. view. onClickListener; 9 import android. widget. textView; 10 11 public class MainActivity extends Activity {12 13 private final int REQUESTCODE_MAIN = 1; 14 15 private final String KEY_MAIN = "Key_main"; 16 private final String KEY_ B = "key_ B"; 17 18 private TextView textView; 19 20 @ Override21 protected void onCreate (Bundle savedInstanceState) {22 super. onCreate (savedInstanceState); 23 setContentView (R. layout. activity_main); 24 25 textView = (TextView) findViewById (R. id. textView_main); 26 27 findViewById (R. id. button_main ). setOnClickListener (new OnClickListener () {28 29 @ Override30 public Void onClick (View v) {31 Intent intent = new Intent (MainActivity. this, ActivityB. class); 32 int a = (int) (Math. random () * 100); 33 int B = (int) (Math. random () * 100); 34 int [] nums = {a, B}; 35 intent. putExtra (KEY_MAIN, nums); 36 startActivityForResult (intent, REQUESTCODE_MAIN); 37} 38}); 39} 40 41 @ Override42 protected void onActivityResult (int requestCode, int resultCode, Intent data) {43 if (data! = Null) {44 Log. d ("-------", "requestCode:" + requestCode + "resultCode:" + resultCode); 45 textView. setText (data. getIntExtra (KEY_ B, 0) + ""); 46} 47} 48}MainActivity. java 1 package com. zzw. teststartintentforrequest; 2 3 import android. app. activity; 4 import android. content. intent; 5 import android. OS. bundle; 6 import android. view. view; 7 import android. view. view. onClickListener; 8 import android. widget. textView; 9 10 public class ActivityB extends Activity {11 12 private final int RESULTCODE_ B = 2; 13 14 private final String KEY_MAIN = "key_main"; 15 private final String KEY_ B = "key_ B "; 16 17 private TextView textView; 18 19 20 @ Override21 protected void onCreate (Bundle savedInstanceState) {22 super. onCreate (savedInstanceState); 23 setContentView (R. layout. activityb); 24 25 textView = (TextView) findViewById (R. id. textView_ B); 26 27 int nums [] = this. getIntent (). getIntArrayExtra (KEY_MAIN); 28 29 final int sum = nums [0] + nums [1]; 30 31 textView. setText (nums [0] + "+" + nums [1] + "=" + sum); 32 33 findViewById (R. id. button_ B ). setOnClickListener (new OnClickListener () {34 35 @ Override36 public void onClick (View v) {37 Intent intent = new Intent (ActivityB. this, MainActivity. class); 38 intent. putExtra (KEY_ B, sum); 39 ActivityB. this. setResult (RESULTCODE_ B, intent); 40 ActivityB. this. finish (); 41} 42}); 43} 44 45 46}ActivityB. java 1 <RelativeLayout xmlns: android = "http://schemas.android.com/apk/res/android" 2 xmlns: tools = "http://schemas.android.com/tools" 3 android: layout_width = "match_parent" 4 android: layout_height = "match_parent" 5 android: background = "@ android: color/holo_blue_light" 6 tools: context = "com. zzw. teststartintentforrequest. mainActivity "> 7 8 <TextView 9 android: id =" @ + id/textView_main "10 android: layout_width =" wrap_content "11 android: layout_height =" wrap_content "12 android: layout_centerInParent = "true" 13 android: text = "0" 14 android: textColor = "@ android: color/holo_red_light" 15 android: textSize = "30sp"/> 16 17 <Button18 android: id = "@ + id/button_main" 19 android: layout_width = "wrap_content" 20 android: layout_height = "wrap_content" 21 android: layout_alignParentBottom = "true" 22 android: layout_centerHorizontal = "true" 23 android: layout_marginBottom = "38dp" 24 android: background = "@ android: color/holo_blue_light "25 android: text =" click to jump to AcyivityB "26 android: textColor =" @ android: color/holo_red_light "/> 27 28 </RelativeLayout>Activity_main.xml 1 <RelativeLayout xmlns: android = "http://schemas.android.com/apk/res/android" 2 xmlns: tools = "http://schemas.android.com/tools" 3 android: layout_width = "match_parent" 4 android: layout_height = "match_parent" 5 android: background = "@ android: color/black" 6 tools: context = "com. zzw. teststartintentforrequest. mainActivity "> 7 8 <TextView 9 android: id =" @ + id/textView_ B "10 android: layout_width =" wrap_content "11 android: layout_height =" wrap_content "12 android: layout_centerInParent = "true" 13 android: textColor = "@ android: color/white" 14 android: textSize = "30sp"/> 15 16 <Button17 android: id = "@ + id/button_ B" 18 android: layout_width = "wrap_content" 19 android: layout_height = "wrap_content" 20 android: textColor = "@ android: color/white "21 android: background =" @ android: color/black "22 android: layout_alignParentBottom =" true "23 android: layout_centerHorizontal =" true "24 android: layout_marginBottom = "38dp" 25 android: text = "click to return MainAcyivity"/> 26 27 </RelativeLayout>Activityb. xml