向下一个Activity传递Boolean ArrayList与String ArrayList遇问题
解决ArrayList传递被覆盖&返回null的问题
这种数据传递时被覆盖、甚至拿不到数据的情况真的太让人头疼了,我之前做项目的时候也踩过类似的坑,咱们一步步来排查解决:
首先排查最容易忽略的点:KEY的一致性
虽然你说KEY不同,但一定要确保发送方和接收方的KEY完全一模一样——大小写、下划线、空格都不能有丝毫差异!比如发送用"EXTRA_STRING_LIST",接收写成"extra_string_list"就会直接拿不到数据,甚至极端情况下可能因为Intent的存储机制导致数据覆盖。
举个正确的发送+接收示例:
发送方代码
// 准备数据 ArrayList<String> stringList = new ArrayList<>(); stringList.add("苹果"); stringList.add("香蕉"); ArrayList<Boolean> booleanList = new ArrayList<>(); booleanList.add(true); booleanList.add(false); // 创建Intent并传递数据 Intent intent = new Intent(this, TargetActivity.class); // 用完全独立的KEY intent.putStringArrayListExtra("EXTRA_STRING_COLLECTION", stringList); intent.putParcelableArrayListExtra("EXTRA_BOOLEAN_COLLECTION", booleanList); startActivity(intent);
接收方代码
// 获取传递的数据 Intent receivedIntent = getIntent(); if (receivedIntent != null) { ArrayList<String> getStrings = receivedIntent.getStringArrayListExtra("EXTRA_STRING_COLLECTION"); ArrayList<Boolean> getBooleans = receivedIntent.getParcelableArrayListExtra("EXTRA_BOOLEAN_COLLECTION"); // 一定要先判空再使用,避免空指针 if (getStrings != null) { // 处理字符串列表 } if (getBooleans != null) { // 处理布尔列表 } }
用Bundle传递时的正确姿势
如果用Bundle传递,绝对不能创建多个Bundle分别放入Intent——第二个Bundle会直接覆盖第一个的内容!正确做法是用同一个Bundle添加所有数据:
正确的Bundle用法
// 发送方 Bundle bundle = new Bundle(); bundle.putStringArrayList("STR_LIST", stringList); bundle.putParcelableArrayList("BOOL_LIST", booleanList); Intent intent = new Intent(this, TargetActivity.class); intent.putExtras(bundle); // 只放一次Bundle startActivity(intent); // 接收方 Bundle receivedBundle = getIntent().getExtras(); if (receivedBundle != null) { ArrayList<String> strList = receivedBundle.getStringArrayList("STR_LIST"); ArrayList<Boolean> boolList = receivedBundle.getParcelableArrayList("BOOL_LIST"); }
错误的Bundle用法(别踩!)
// 错误示例:两次putExtras会导致第一个Bundle被覆盖 Bundle bundle1 = new Bundle(); bundle1.putStringArrayList("STR_LIST", stringList); intent.putExtras(bundle1); Bundle bundle2 = new Bundle(); bundle2.putParcelableArrayList("BOOL_LIST", booleanList); intent.putExtras(bundle2); // 这里bundle1的内容被完全覆盖了!
关于boolean数组的问题
如果你改用boolean数组传递,要注意用基本类型数组对应的Intent方法,别和Parcelable混淆:
// 发送boolean数组 boolean[] boolArray = {true, false, true}; intent.putExtra("EXTRA_BOOL_ARRAY", boolArray); // 接收数组 boolean[] receivedArray = getIntent().getBooleanArrayExtra("EXTRA_BOOL_ARRAY");
如果误用putParcelableArrayExtra传递基本类型数组,肯定会拿到null。
返回数据为null的额外排查点
如果是通过startActivityForResult(或新的Activity Result API)返回数据,要注意:
- 目标Activity必须调用
setResult(RESULT_OK, resultIntent)后再finish,不能直接finish - 发起方的回调中要先判断
resultCode == RESULT_OK,再去获取数据(用户按返回键时resultCode是RESULT_CANCELED,数据会是null)
示例:
目标Activity返回数据
// 准备返回数据 ArrayList<String> resultStrings = new ArrayList<>(); resultStrings.add("返回测试"); Intent resultIntent = new Intent(); resultIntent.putStringArrayListExtra("RESULT_STR", resultStrings); setResult(RESULT_OK, resultIntent); // 必须设置RESULT_OK finish();
发起方接收返回数据
// 旧版startActivityForResult回调 @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == YOUR_REQUEST_CODE && resultCode == RESULT_OK && data != null) { ArrayList<String> result = data.getStringArrayListExtra("RESULT_STR"); // 处理返回数据 } }
最后再检查一个小细节
如果是在Fragment中传递数据,要确保获取Intent的方式正确:用getActivity().getIntent(),或者如果是Fragment之间通过setArguments传递,就用getArguments()获取,别把两种方式搞混。
如果以上方法都试过还是有问题,可以把你的发送和接收代码贴出来,咱们再针对性排查!
内容的提问来源于stack exchange,提问作者Anzupe
相关产品推荐
相关产品推荐

