Android——2个activity之间的数据传递返回
老版本2个activity之间传递反回数据,实例如:https://www.cnblogs.com/xiaobaibailongma/p/16440432.html
新版本如下:https://blog.csdn.net/freezingxu/article/details/124953918?spm=1001.2101.3001.6650.2&utm_medium=distribute.pc_relevant.none-task-blog-2%7Edefault%7EBlogCommendFromBaidu%7ERate-2-124953918-blog-133701756.235%5Ev40%5Epc_relevant_3m_sort_dl_base3&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2%7Edefault%7EBlogCommendFromBaidu%7ERate-2-124953918-blog-133701756.235%5Ev40%5Epc_relevant_3m_sort_dl_base3&utm_relevant_index=3
在需要调用系统Intent之前,我们需要定义所需的Launcher:
1、protected ActivityResultLauncher activityResultLauncher;
2、protected ActivityResultLauncher takePhotoLauncher;
3、protected ActivityResultLauncher selectImageLauncher;
4、protected ActivityResultLauncher selectFileLauncher;
此处我定义了4个不同的Launcher,分别用来处理普通的Intent跳转、拍照、选择相册中已有的图片和选择手机中的文件。
activityResultLauncher:处理普通的Intent跳转,并携带参数往返;
takePhotoLauncher:打开照相机进行拍照,并获得照片;
selectImageLauncher:打开系统Intent选择手机上的图片;
selectFileLauncher:打开系统Intent选择手机里的文件。
原文链接:https://blog.csdn.net/freezingxu/article/details/124953918
=============================================================================
第一个布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:id="@+id/tv_request" android:layout_width="match_parent" android:layout_height="wrap_content" android:paddingLeft="5dp" android:paddingTop="5dp" android:textColor="#000000" android:textSize="17sp" /> <Button android:id="@+id/btn_request" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:text="传送请求数据" android:textColor="#000000" android:textSize="17sp" /> <TextView android:id="@+id/tv_response" android:layout_width="match_parent" android:layout_height="wrap_content" android:paddingLeft="5dp" android:textColor="#000000" android:textSize="17sp" /> </LinearLayout>
activity:
package com.example.myapplication; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.TextView; import androidx.activity.result.ActivityResultLauncher; import androidx.activity.result.contract.ActivityResultContracts; import androidx.appcompat.app.AppCompatActivity; public class MainActivity extends AppCompatActivity implements View.OnClickListener { private String mRrequest = "你吃饭了吗?来我家吃吧"; private TextView tv_response; // 声明一个文本视图对象 private ActivityResultLauncher mLauncher; // 声明一个活动结果启动器对象 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_register_result); // 从布局文件中获取名叫tv_request的文本视图 TextView tv_request = findViewById(R.id.tv_request); tv_request.setText("待发送的消息为:"+mRrequest); // 从布局文件中获取名叫tv_response的文本视图 tv_response = findViewById(R.id.tv_response); findViewById(R.id.btn_request).setOnClickListener(this); // 注册一个善后工作的活动结果启动器 mLauncher = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(), result -> { if (result.getResultCode()==RESULT_OK && result.getData()!=null) { Bundle bundle = result.getData().getExtras(); // 从返回的意图中获取快递包裹 // 从包裹中取出名叫response_time的字符串 String response_time = bundle.getString("response_time"); // 从包裹中取出名叫response_content的字符串 String response_content = bundle.getString("response_content"); String desc = String.format("收到返回消息:\n应答时间为:%s\n应答内容为:%s", response_time, response_content); tv_response.setText(desc); // 把返回消息的详情显示在文本视图上 } }); } @Override public void onClick(View v) { if (v.getId() == R.id.btn_request) { // 创建一个意图对象,准备跳到指定的活动页面 Intent intent = new Intent(this, ActResponseActivity.class); Bundle bundle = new Bundle(); // 创建一个新包裹 // 往包裹存入名叫request_time的字符串 bundle.putString("request_time", DateUtil.getNowTime()); // 往包裹存入名叫request_content的字符串 bundle.putString("request_content", mRrequest); intent.putExtras(bundle); // 把快递包裹塞给意图 mLauncher.launch(intent); // 活动结果启动器开动了 } } }
第二个布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:id="@+id/tv_request" android:layout_width="match_parent" android:layout_height="wrap_content" android:paddingLeft="5dp" android:paddingTop="5dp" android:textColor="#000000" android:textSize="17sp" /> <Button android:id="@+id/btn_response" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:text="返回应答数据" android:textColor="#000000" android:textSize="17sp" /> <TextView android:id="@+id/tv_response" android:layout_width="match_parent" android:layout_height="wrap_content" android:paddingLeft="5dp" android:textColor="#000000" android:textSize="17sp" /> </LinearLayout>
第二个activity:
package com.example.myapplication; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.TextView; import androidx.appcompat.app.AppCompatActivity; public class ActResponseActivity extends AppCompatActivity implements View.OnClickListener { private String mResponse = "我吃过了,还是你来我家吃"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_act_response); // 从布局文件中获取名叫tv_request的文本视图 TextView tv_request = findViewById(R.id.tv_request); findViewById(R.id.btn_response).setOnClickListener(this); // 从布局文件中获取名叫tv_response的文本视图 TextView tv_response = findViewById(R.id.tv_response); tv_response.setText("待返回的消息为:"+mResponse); // 从上一个页面传来的意图中获取快递包裹 Bundle bundle = getIntent().getExtras(); // 从包裹中取出名叫request_time的字符串 String request_time = bundle.getString("request_time"); // 从包裹中取出名叫request_content的字符串 String request_content = bundle.getString("request_content"); String desc = String.format("收到请求消息:\n请求时间为:%s\n请求内容为:%s", request_time, request_content); tv_request.setText(desc); // 把请求消息的详情显示在文本视图上 } @Override public void onClick(View v) { if (v.getId() == R.id.btn_response) { Intent intent = new Intent(); // 创建一个新意图 Bundle bundle = new Bundle(); // 创建一个新包裹 // 往包裹存入名叫response_time的字符串 bundle.putString("response_time", DateUtil.getNowTime()); // 往包裹存入名叫response_content的字符串 bundle.putString("response_content", mResponse); intent.putExtras(bundle); // 把快递包裹塞给意图 // 携带意图返回上一个页面。RESULT_OK表示处理成功 setResult(Activity.RESULT_OK, intent); finish(); // 结束当前的活动页面 } } }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· .NET10 - 预览版1新功能体验(一)
2021-01-10 python+Selenium WebDriver——WebUI自动化——背诵整理