Activity A 中启动 Activity B 并通过 Intent传递一些数据到B,那么B的数据怎么在返回给A。
Android 提供了startActivityForResult方法。
例子:
activity_comm_1.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView android:text="请随便说点什么"
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"></TextView>
<Button android:text="编辑"
android:id="@+id/edit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"></Button>
</LinearLayout>
Activity_comm_1.java:
package com.demo.intent;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class Activity_comm_1 extends Activity {
public static int start_flag = 1;
TextView text = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_comm_1);
Button startButton = (Button) findViewById(R.id.edit);
startButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent i = new Intent(Activity_comm_1.this,
Activity_comm_2.class);
Bundle bd = new Bundle();
text = (TextView) findViewById(R.id.textView1);
bd.putString("content", (String) text.getText());
i.putExtra("attachment", bd);
startActivityForResult(i, start_flag);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == start_flag) {
if (resultCode == RESULT_OK) {
Bundle b = data.getBundleExtra("attachment2");
text.setText(b.getString("newcontent"));
} else {
return;
}
}
}
}
activity_comm_2.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText android:text="EditText" android:id="@+id/editText1"
android:layout_width="match_parent" android:layout_height="wrap_content"
android:layout_weight="1"></EditText>
<Button android:text="保存" android:id="@+id/save"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"></Button>
<Button android:text="放弃" android:id="@+id/abort"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"></Button>
</LinearLayout>
Activity_comm_2.java:
package com.demo.intent;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class Activity_comm_2 extends Activity {
EditText text = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_comm_2);
text = (EditText) findViewById(R.id.editText1);
// 从Intent读取信息
Intent i = getIntent();
Bundle b = i.getBundleExtra("attachment");
String temp = b.getString("content");
text.setText(temp);
// setup button listener
Button saveButton = (Button) findViewById(R.id.save);
saveButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent i =new Intent(Activity_comm_2.this,Activity_comm_1.class);
Bundle bd = new Bundle();
bd.putString("newcontent", String.valueOf(text.getText()));
i.putExtra("attachment2", bd);
setResult(RESULT_OK, i);
finish();
}
});
Button abortButton = (Button) findViewById(R.id.abort);
abortButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
setResult(RESULT_CANCELED);
finish();
}
});
}
}