废话不多说了,直接给大家贴代码了。
package com.example.testactivityresquest;
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.Toast;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn = (Button) findViewById(R.id.button);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, Activityb.class);
int[] nums = { , };
intent.putExtra(Changliang.KEY, nums);
//有别于startActivity,如果启动的其他Activity多了以后。相当于定一个特定KEY值,返回根据KEY值返回。
startActivityForResult(intent, Changliang.requestCode);
}
});
}
//Activityb传回来的数据在这个方法中获取
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
int s = data.getIntExtra(Changliang.Activity_b_KEY, );
Toast.makeText(getApplicationContext(), "传递两个数得到的和是:" + s, ).show();
}
}
package com.example.testactivityresquest;
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.Toast;
public class Activityb extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activityb);
Intent intent = this.getIntent();
int[] n = intent.getIntArrayExtra(Changliang.KEY);
final int nums = n[] + n[];
Toast.makeText(this, n[] + " " + n[], ).show();
Button btn = (Button) findViewById(R.id.button);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(Activityb.this, MainActivity.class);
intent.putExtra(Changliang.Activity_b_KEY, nums);
// 将数据根据特定键值的意图事件导入
Activityb.this.setResult(Changliang.requestCode, intent);
//关闭后返回主Activity
Activityb.this.finish();
}
});
}
}
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文件:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ff" >
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="dp"
android:text="启动Activityb" />
</RelativeLayout>
<?xml version="." encoding="utf-"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ff"
android:orientation="vertical" >
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="返回Activity" />
</LinearLayout>
别忘在AndroidManifast中注册activityb。
运行效果图:

startActivityForResult与startActivity的不同之处在于:
1、startActivity( )
仅仅是跳转到目标页面,若是想跳回当前页面,则必须再使用一次startActivity( )。
2、startActivityForResult( )
可以一次性完成这项任务,当程序执行到这段代码的时候,假若从T1Activity跳转到下一个Text2Activity,而当这个Text2Activity调用了finish()方法以后,程序会自动跳转回T1Activity,并调用前一个T1Activity中的onActivityResult( )方法。