实现Activity数据中间的协同
16lz
2021-01-25
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();}});}}
更多相关文章
- “罗永浩抖音首秀”销售数据的可视化大屏是怎么做出来的呢?
- Nginx系列教程(三)| 一文带你读懂Nginx的负载均衡
- 不吹不黑!GitHub 上帮助人们学习编码的 12 个资源,错过血亏...
- 再集成Qt4.7
- Android(安卓)Handler类消息传递机制(三)不同类中使用Handler
- Android(安卓)SensorService源码分析(二)
- Android从服务器端接收json数据并解析的代码
- [Android开发]Android(安卓)重要组件 之 Intent
- Android(安卓)Jackson 概述