在一个Activity中通过意图启动另一个Activity,并且希望被启动的Activity回传结果过去,这时候就需要startActivityForResult(intent)来调用Activity, 并且重写onActivityResult()方法,它会被回调,做接收结果的一些操作。
下面通过一个实例来演示如何使用:
工程结构:
运行结果:
MainActivity结果截图:
打开的OtherActivity截图:
输入需要回传的内容:
返回结果截图:
下面是源代码:
MainActivity.java
package com.intent.activity;
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.TextView;
public class MainActivity extends Activity implements OnClickListener{
private Button btn;
private TextView backText;
private static final int REQUESTCODE = 1; //返回的结果码
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
backText = (TextView)findViewById(R.id.backText);
btn = (Button)findViewById(R.id.btOpenOtherActivity);
btn.setOnClickListener(this);
}
@Override
public void onClick(View v) {
//定义一个意图
Intent intent = new Intent(MainActivity.this,OtherActivity.class);
//启动意图
startActivityForResult(intent, REQUESTCODE);
}
//activity的回调函数
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(resultCode==2){
if(requestCode==REQUESTCODE){
String message = data.getStringExtra("message");
backText.setText("回传过来的值:"+message);
}
}
}
}
OtherActivity.java
package com.intent.activity;
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.EditText;
public class OtherActivity extends Activity implements OnClickListener{
private EditText editText;
private Button btn;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.other);
editText = (EditText) findViewById(R.id.editText);
btn = (Button)findViewById(R.id.btnSendBack);
btn.setOnClickListener(this);
//获取Intent传递的Bundle对象和它里面的数据
Bundle data = getIntent().getExtras();
}
@Override
public void onClick(View v) {
Intent intent = new Intent();
String text = editText.getText().toString();
intent.putExtra("message", text);
//通过Intent对象返回结果,setResult方法
setResult(2,intent);
finish();//结束当前Acitvity
}
}
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="这是:MainActivity" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btOpenOtherActivity"
android:text="使用意图打开OtherActivity并返回结果"/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/backText" />
</LinearLayout>
other.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="请输入回传的内容"
android:id="@+id/editText"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="返回结果"
android:id="@+id/btnSendBack"/>
</LinearLayout>