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 @Override 14 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 @Override 22 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 //有别于startActivity,如果启动的其他Activity多了以后。相当于定一个特定KEY值,返回根据KEY值返回。 28 startActivityForResult(intent, Changliang.requestCode); 29 30 } 31 }); 32 } 33 //Activityb传回来的数据在这个方法中获取 34 @Override 35 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(), "传递两个数得到的和是:" + 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 @Override 14 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 @Override 29 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 // 将数据根据特定键值的意图事件导入 35 Activityb.this.setResult(Changliang.requestCode, intent1); 36 //关闭后返回主Activity 37 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文件:
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="启动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="返回Activity" /> 13 14 </LinearLayout>
别忘在AndroidManifast中注册activityb。
运行效果图: