温馨提示×

android parcel传递数组的方法是什么

小亿
83
2024-03-18 18:05:33
栏目: 编程语言

在Android中,可以使用Bundle类来传递数组。首先,将数组放入Bundle中,然后将Bundle传递给Intent对象,最后在接收方从Intent对象中获取Bundle并取出数组。

示例代码如下:

发送方:

String[] array = {"item1", "item2", "item3"};
Bundle bundle = new Bundle();
bundle.putStringArray("myArray", array);

Intent intent = new Intent(this, ReceiverActivity.class);
intent.putExtras(bundle);
startActivity(intent);

接收方:

Bundle bundle = getIntent().getExtras();
String[] array = bundle.getStringArray("myArray");
// 现在你可以使用获取的数组array进行操作

0