当前位置: 首页 > news >正文

Android 开发中,Intent 和 Bundle 组件间传递数据的几种方式

模式 1:直接通过 Intent 传递数据(最简单)​

发送方
Intent intent = new Intent(MainActivity.this, TargetActivity.class);
intent.putExtra("key_string", "文本数据");
intent.putExtra("key_int", 100);
intent.putExtra("key_boolean", true);
startActivity(intent);

接收方

Intent intent = getIntent();
String text = intent.getStringExtra("key_string");
int number = intent.getIntExtra("key_int", 0); // 0是默认值
boolean flag = intent.getBooleanExtra("key_boolean", false);

 

模式 2:通过 Bundle 打包传递(适合批量数据)​

发送方
Intent intent = new Intent(MainActivity.this, TargetActivity.class);
Bundle bundle = new Bundle();
bundle.putString("key_string", "文本数据");
bundle.putInt("key_int", 100);
intent.putExtras(bundle); // 关键!将Bundle附加到Intent
startActivity(intent);

接收方

Bundle bundle = getIntent().getExtras();
if (bundle != null) {String text = bundle.getString("key_string");int number = bundle.getInt("key_int", 0);
}

 

模式 3:传递复杂对象(需实现 Parcelable 或 Serializable)​

对象类实现 Parcelable(推荐)​
public class User implements Parcelable {private String name;private int age;// 构造方法、Getter/Setter 省略...@Overridepublic void writeToParcel(Parcel dest, int flags) {dest.writeString(name);dest.writeInt(age);}public static final Creator<User> CREATOR = new Creator<User>() {@Overridepublic User createFromParcel(Parcel in) {return new User(in.readString(), in.readInt());}};
}

 发送方

User user = new User("张三", 25);
Intent intent = new Intent(this, TargetActivity.class);
intent.putExtra("key_user", user); // 直接传递对象
startActivity(intent);

 接收方

User user = getIntent().getParcelableExtra("key_user");

模式 4:传递数组或集合

发送方
Intent intent = new Intent(this, TargetActivity.class);
// 传递String数组
intent.putExtra("key_array", new String[]{"A", "B", "C"});
// 传递ArrayList(需指定泛型)
ArrayList<Integer> list = new ArrayList<>(Arrays.asList(1, 2, 3));
intent.putIntegerArrayListExtra("key_list", list);
startActivity(intent);
接收方
String[] array = getIntent().getStringArrayExtra("key_array");
ArrayList<Integer> list = getIntent().getIntegerArrayListExtra("key_list");

模式 5:通过 Bundle 传递二进制数据(如文件字节流)​

发送方
byte[] fileData = getFileBytes(); // 获取文件的字节数组
Intent intent = new Intent(this, TargetActivity.class);
Bundle bundle = new Bundle();
bundle.putByteArray("key_file_data", fileData);
intent.putExtras(bundle);
startActivity(intent);
接收方
Bundle bundle = getIntent().getExtras();
if (bundle != null) {byte[] fileData = bundle.getByteArray("key_file_data");// 处理二进制数据...
}

最佳实践总结

  1. 简单数据​:直接用 Intent.putExtra()
  2. 批量数据​:用 Bundle 打包后传递。
  3. 对象传递​:优先实现 Parcelable(性能优于 Serializable)。
  4. 集合数据​:使用专用方法如 putStringArrayListExtra()
  5. 二进制数据​:用 putByteArray() 避免编码问题。

关键注意事项​:

  • 接收方始终检查 Intent 或 Bundle 是否为 null
  • 键名(如 "key_string")需保持发送和接收端一致。
  • 跨进程传输时,Bundle 大小限制为 ​1MB​(Android 8.0+)。

 

http://www.xdnf.cn/news/1002439.html

相关文章:

  • 基于Node.js的线上教学系统的设计与实现(源码+论文+调试+安装+售后)
  • 如何“下载安转Allure”?
  • #pragma pack的作用
  • 海外广告投放|FB IG 速推帖子有效吗?
  • 2.倒排索引
  • Mitsubishi GX Works3 / GOT3 的惡意工程混淆邏輯注入攻擊
  • Parasoft C++Test软件集成测试(部件测试)_实例讲解
  • C++的学习路径
  • 第一个简单的爬虫
  • 一起了解--CAST函数
  • C++上学抄近路 动态规划算法实现 CCF信息学奥赛C++ 中小学普及组 CSP-J C++算法案例学习
  • Spring Boot 项目中如何划分事务边界,避免长事务?
  • yolo11学习笔记
  • ajax访问阿里云天气接口,获取7天天气
  • C++ 引用
  • get_attribute的使用方法
  • 【小根堆】P9557 [SDCPC 2023] Building Company|普及+
  • Spring Cloud Gateway + OAuth2 + JWT 单点登录(SSO)实现方案
  • Java八股文——MySQL「SQL 基础篇」
  • 随记:sw2urdf插件导出urdf模型在ROS2-rviz2显示
  • 在Vue2项目中引入ElementUI详细步骤
  • Linux系统下安装elasticsearch6.8并配置ik分词
  • 【Java】浅谈ScheduledThreadPoolExecutor
  • Python实战应用-Python实现Web请求与响应
  • 智能合约的浪潮:从区块链到业务自动化的 IT 新引擎
  • 服务器-客户端下kafka的消息处理流程
  • Vue3+PDF.js 实现高性能 PDF 阅读器开发实战
  • C# 动态管理控件和事件,批量查询管理同类控件
  • JavaWeb期末速成 JSP
  • 浅谈DaemonSet