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;@Overridepublic 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);}});}@Overrideprotected 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;@Overridepublic 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 listenerButton 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();}});}}


更多相关文章

  1. “罗永浩抖音首秀”销售数据的可视化大屏是怎么做出来的呢?
  2. Nginx系列教程(三)| 一文带你读懂Nginx的负载均衡
  3. 不吹不黑!GitHub 上帮助人们学习编码的 12 个资源,错过血亏...
  4. 再集成Qt4.7
  5. Android(安卓)Handler类消息传递机制(三)不同类中使用Handler
  6. Android(安卓)SensorService源码分析(二)
  7. Android从服务器端接收json数据并解析的代码
  8. [Android开发]Android(安卓)重要组件 之 Intent
  9. Android(安卓)Jackson 概述

随机推荐

  1. Android收藏商品/关注商品
  2. android 进程内存分析
  3. android 窗口如何靠左和靠右边框布局
  4. Android解析Xml字符串例子
  5. android 搭建http服务器AndroidAsync
  6. vs2015 支持Android arm neon Introducin
  7. 7个Android应用程序源代码
  8. android图片转换代码
  9. Android监听屏幕锁屏
  10. Android 中使用MediaRecorder进行录像详