The startActivityForResult method is used to transmit data between two activities ., Startactivityresult

Source: Internet
Author: User

The startActivityForResult method is used to transmit data between two activities ., Startactivityresult

1 package com. example. testactivityresquest; 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. button; 9 import android. widget. toast; 10 11 public class MainActivity extends Activity {12 13 @ Override14 protected void onCreate (Bundle savedInstanceState) {15 super. OnCreate (savedInstanceState); 16 setContentView (R. layout. activity_main); 17 18 Button btn = (Button) findViewById (R. id. button1); 19 btn. setOnClickListener (new OnClickListener () {20 21 @ Override22 public void onClick (View v) {23 24 Intent intent = new Intent (MainActivity. this, Activityb. class); 25 int [] nums = {15, 15}; 26 intent. putExtra (Changliang. KEY, nums); 27 // different from startActivity.. Returns a specific KEY value. 28 startActivityForResult (intent, Changliang. requestCode); 29 30} 31}); 32} 33 // data returned by Activityb obtain 34 @ Override35 protected void onActivityResult (int requestCode, int resultCode, intent data) {36 37 int s = data. getIntExtra (Changliang. activity_ B _KEY, 1); 38 39 Toast. makeText (getApplicationContext (), "the sum obtained by passing two numbers is:" + s, 1 ). show (); 40 41} 42 43}
1 package com. example. testactivityresquest; 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. button; 9 import android. widget. toast; 10 11 public class Activityb extends Activity {12 13 @ Override14 protected void onCreate (Bundle savedInstanceState) {15 super. onCreate (savedInstanceState); 16 setContentView (R. layout. activityb); 17 18 Intent intent = this. getIntent (); 19 int [] n = intent. getIntArrayExtra (Changliang. KEY); 20 21 final int nums = n [0] + n [1]; 22 23 Toast. makeText (this, n [0] + "" + n [1], 1 ). show (); 24 25 Button btn = (Button) findViewById (R. id. button2); 26 btn. setOnClickListener (new OnClickListener () {27 28 @ Override29 public void onClick (View v) {30 31 Intent intent1 = new Intent (Activityb. this, MainActivity. class); 32 33 intent1.putExtra (Changliang. activity_ B _KEY, nums); 34 // import data to 35 Activityb Based on the intent event of the specific key value. this. setResult (Changliang. requestCode, intent1); 36 // return the main Activity37 Activityb. this. finish (); 38 39} 40}); 41} 42 43}
package com.example.testactivityresquest;public class Changliang {    public static final String KEY="key";public static final String Activity_b_KEY="key1";public static final int requestCode=1987;}

Xml file:

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 = "# ff0000"> 6 7 <Button 8 android: id = "@ + id/button1" 9 android: layout_width = "wrap_content" 10 android: layout_height = "wrap_content" 11 android: layout_alignParentTop = "true" 12 android: layout_centerHorizontal = "true" 13 android: layout_marginTop = "88dp" 14 android: text = "Start Activityb"/> 15 16 </RelativeLayout>
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: background = "#00ff00" 6 android: orientation = "vertical"> 7 8 <Button 9 android: id = "@ + id/button2" 10 android: layout_width = "wrap_content" 11 android: layout_height = "wrap_content" 12 android: text = "Return Activity"/> 13 14 </LinearLayout>

Do not forget to register activityb in AndroidManifast.

Run:

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.