Android:startActivityForResult返回后原Activity未被调用问题排查
兄弟,我一眼就瞅到俩关键坑,加起来刚好就是你遇到的问题!
核心问题分析
1. Intent 传大数据触发系统静默回收
Android对Intent里的Extras大小有隐形限制,一般在1MB左右(不同厂商可能微调)。你把2张及以上的图片转成byte[]直接塞Intent里,总数据量很容易超过这个阈值。系统发现后会悄悄干掉你的原Activity(不会打崩溃日志,属于系统的“合规回收”),所以从图片页面返回时,原Activity已经没了——系统要么尝试重建它(重建失败就黑屏),要么直接回到栈里更早的Activity,这就是你看到的现象。
2. onActivityResult 拼写错误!回调根本没执行
看你贴的代码,重写的方法名是onAcitivtyResult——中间的Activity少了个t!系统认不出这是回调方法,所以哪怕正常返回,这个方法也不会被调用,自然看不到日志。这绝对是你看不到onAcitivtyResult日志的直接原因!
解决方案
第一步:先把方法名改对!
把方法名改成正确的onActivityResult,还要记得调用父类的方法:
@Override protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) { super.onActivityResult(requestCode, resultCode, data); // 别漏这句! Log.d(TAG,"onActivityResult"); // 现在正常返回的话日志就能打出来了 // ... 你的业务逻辑 }
第二步:解决大数据传递的问题
别再用Intent传byte[]了,推荐两种靠谱方案:
方案A:用临时文件存图片
- 在图片添加页面,把图片存到App的缓存目录,然后传文件路径:
// 获取App内部缓存目录 File tempDir = getCacheDir(); // 遍历hashMap存文件 for (Map.Entry<String, byte[]> entry : hashMap.entrySet()) { String key = entry.getKey(); byte[] imageBytes = entry.getValue(); File tempFile = new File(tempDir, "temp_img_" + key + ".jpg"); try (FileOutputStream fos = new FileOutputStream(tempFile)) { fos.write(imageBytes); } catch (IOException e) { e.printStackTrace(); } // 把文件路径塞Intent returnIntent.putExtra(key, tempFile.getAbsolutePath()); } setResult(Activity.RESULT_OK, returnIntent); finish();
- 在原Activity里读文件,用完记得删临时文件:
if(requestCode == 33 && resultCode == Activity.RESULT_OK && data != null){ Bundle bundle = data.getExtras(); List<ProductImage> productImageList = new ArrayList<>(); File tempDir = getCacheDir(); for (Integer i=1;i<=3;i++){ String path = bundle.getString(i.toString()); if (path != null){ File tempFile = new File(path); if (tempFile.exists()){ // 读文件转byte[] byte[] imageBytes = Files.readAllBytes(tempFile.toPath()); ProductImage productImage = new ProductImage(); productImage.setImageBytes(imageBytes); productImageList.add(productImage); // 删临时文件,避免占空间 tempFile.delete(); } } } addProductViewModel.fillUpMutableLiveData(productImageList); addProductViewModel.updateCurrentId(0); }
方案B:用ViewModel跨页面共享数据
如果两个Activity在同一个任务栈里,用ViewModel传数据更省心,还能避免Activity重建丢数据:
- 先整个共享ViewModel:
public class ImageShareViewModel extends ViewModel { private MutableLiveData<List<byte[]>> imageBytesList = new MutableLiveData<>(); public void setImageBytesList(List<byte[]> list) { imageBytesList.setValue(list); } public LiveData<List<byte[]>> getImageBytesList() { return imageBytesList; } }
- 图片添加页面存数据:
ImageShareViewModel viewModel = new ViewModelProvider(this).get(ImageShareViewModel.class); List<byte[]> imageList = new ArrayList<>(); if (hashMap.containsKey("one")) imageList.add(hashMap.get("one")); if (hashMap.containsKey("two")) imageList.add(hashMap.get("two")); if (hashMap.containsKey("three")) imageList.add(hashMap.get("three")); viewModel.setImageBytesList(imageList); setResult(Activity.RESULT_OK); finish();
- 原Activity监听数据:
// 在onCreate里初始化 ImageShareViewModel viewModel = new ViewModelProvider(this).get(ImageShareViewModel.class); viewModel.getImageBytesList().observe(this, imageBytesList -> { List<ProductImage> productImageList = new ArrayList<>(); for (byte[] bytes : imageBytesList) { ProductImage productImage = new ProductImage(); productImage.setImageBytes(bytes); productImageList.add(productImage); } addProductViewModel.fillUpMutableLiveData(productImageList); addProductViewModel.updateCurrentId(0); });
额外要检查的点
- 看看原Activity的Manifest配置里有没有加
android:noHistory="true",加了的话系统会在离开后立刻回收它,也会导致类似问题。 - 如果非要用Intent传,那先把图片压到最小(降分辨率、降质量),但多张的话还是容易超,不推荐这种方案。
内容的提问来源于stack exchange,提问作者AskQuestions
相关产品推荐
相关产品推荐

