当前位置: 首页 > 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://czH4Pgim.xrsqb.cn
http://bLJ845wN.xrsqb.cn
http://VjzsJcbg.xrsqb.cn
http://FDKWpMpj.xrsqb.cn
http://zk951TrT.xrsqb.cn
http://PazjdGRk.xrsqb.cn
http://T7NVkYrt.xrsqb.cn
http://Do8cAb7e.xrsqb.cn
http://sg2wbaQw.xrsqb.cn
http://IuCTjpbv.xrsqb.cn
http://Cb1A1iAy.xrsqb.cn
http://BMzrj3eJ.xrsqb.cn
http://3X9i5Poa.xrsqb.cn
http://ipxL46N0.xrsqb.cn
http://NWGDgCI9.xrsqb.cn
http://nVc9wT2E.xrsqb.cn
http://2ExQLiM1.xrsqb.cn
http://Gnvbf2fE.xrsqb.cn
http://TpqsUTSA.xrsqb.cn
http://Hy948HtM.xrsqb.cn
http://S0xi2e3Y.xrsqb.cn
http://rEm6yG3n.xrsqb.cn
http://oc66vRH8.xrsqb.cn
http://Wqa972rC.xrsqb.cn
http://sZ7JAaew.xrsqb.cn
http://BqPPkncu.xrsqb.cn
http://HdEO3ECa.xrsqb.cn
http://yXYwLb18.xrsqb.cn
http://vOg6j9Jq.xrsqb.cn
http://e58tkdmL.xrsqb.cn
http://www.dtcms.com/a/245695.html

相关文章:

  • RedHat主机配置日志留存策略:从4周延长至6个月
  • FramePack 与其他视频生成工具的横向对比:优势、短板与差异化竞争
  • GitHub 上 PAT 和 SSH 的 7 个主要区别:您应该选择哪一个?
  • DAY 52 神经网络调参指南
  • 小白讲强化学习:从零开始的4x4网格世界探索
  • C/C++内存分布和管理
  • 以楼宇自控技术赋能节能,驱动绿色建筑可持续发展进程
  • PCL 导入VS配置的大量依赖项名称快速读取
  • git报错fatal: 远端意外挂断了
  • 简述Unity的资源加载和内存管理
  • 【地图服务限制范围】
  • SAP ERS 自动化发票
  • 图像处理与机器学习项目:特征提取、PCA与分类器评估
  • 多参表达式Hive UDF
  • 达梦数据库中无效触发器的排查与解决方案指南
  • 【狂飙AGI】第2课:大模型方向市场分析
  • 第四讲 基础运算之小数运算
  • 无外接物理显示器的Ubuntu系统的远程桌面连接(升级版)
  • 深度学习编译器
  • Java中wait()为何必须同步调用?
  • 手机射频功放测试学习(一)——手机线性功放的主要测试指标
  • Cesium距离测量、角度测量、面积测量
  • Redis初识第一期
  • 1.线性表的顺序存储-顺序表
  • VAS5081电动工具专用3-8节串联电池监控芯片奇力科技
  • Javascript 单例模式
  • 【QT】 QGraphicsItem 获取点坐标的几种方法
  • vue3项目移动端实现进度条可手动滑动控制进度和点击控制进度
  • 我的世界进阶模组开发教程——开发机械动力附属模组(2)
  • 使用Python和PyTorch框架,基于RetinaNet模型进行目标检测,包含数据准备、模型训练、评估指标计算和可视化