You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android中从Activity#2返回后onActivityResult未触发的问题

Activity返回数据无法触发onActivityResult的问题

场景说明

我有两个Activity:

  • Activity #1:SetListMasterViewActivity
  • Activity #2:SetListOpenViewActivity

Activity #1可正常通过Intent启动Activity #2,用户点击Activity #1歌曲列表中的歌曲,会在Activity #2查看歌词。需求是:用户点击Activity #2的返回按钮时,将当前查看的歌曲信息传回Activity #1,实现该歌曲的高亮效果。

目前遇到的问题:在Activity #2的返回按钮点击事件中,已通过setResult(RESULT_OK, resultIntent)设置返回数据,随后调用finish()并启动Activity #1,但Activity #1的onActivityResult方法完全未触发,相关日志无输出,无法接收返回数据。

已尝试的操作

  • 已使用setResult(RESULT_OK, resultIntent)设置返回结果
  • 尝试过已废弃的startActivityForResult方法,Android Studio提示该方法过时
  • 将onActivityResult中的代码移至onResume方法,仍无法获取Intent数据

相关代码

Activity #2 返回按钮点击事件代码

backButton.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        int currentPosition = getCurrentPosition(songName, songsList);
        String currentSongName = songsList.get(currentPosition);
        int currentSongId = getSongIdByName(currentSongName);
        Intent resultIntent = new Intent();
        resultIntent.putExtra("lastViewedSongId", currentSongId);
        resultIntent.putExtra("lastViewedSongName", currentSongName);
        setResult(RESULT_OK, resultIntent);
        finish();
        startSetListMasterViewActivity();
    }
});

Activity #2 启动Activity #1的方法

private void startSetListMasterViewActivity() {
    Intent intent = new Intent(SetListOpenViewActivity.this, SetListMasterViewActivity.class);
    Log.d("SttList", "(SetListOpenViewActivity) line #194 - Launching SetListMasterViewActivity");
    setListMasterViewLauncher.launch(intent);
}

Activity #2 的ActivityResultLauncher

private final ActivityResultLauncher<Intent> setListMasterViewLauncher = registerForActivityResult(
        new ActivityResultContracts.StartActivityForResult(),
        result -> {
            Log.d("SttList", "(SetListOpenViewActivity) line #208 - ActivityResultLauncher");
            if (result.getResultCode() == Activity.RESULT_OK) {
                // ...
            }
        }
);

Activity #1 接收数据的onActivityResult方法

@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    Log.d("SttList", "(SetListMasterViewActivity) line #191 - onActivityResult called");
    Log.d("SttList", "(SetListMasterViewActivity) line #192 - requestCode: " + requestCode + ", resultCode: " + resultCode);
    if (requestCode == REQUEST_CODE_SET_LIST_MASTER && resultCode == RESULT_OK && data != null) {
        int lastViewedSongId = data.getIntExtra("lastViewedSongId", -1);
        String lastViewedSongName = data.getStringExtra("lastViewedSongName");
        int position = songNames.indexOf(lastViewedSongName);
        Log.d("SttList", "(SetListMasterViewActivity) line #201 - Received lastViewedSongId: " + lastViewedSongId);
        Log.d("SttList", "(SetListMasterViewActivity) line #202 - Received lastViewedSongName: " + lastViewedSongName);
        if (position != -1) {
            TextView songTextView = (TextView) songsListView.getChildAt(position);
            songTextView.setBackgroundColor(Color.YELLOW);
        }
    }
}

问题解决思路

核心错误点

代码的关键错误是:在Activity #2中调用finish()后又手动启动了Activity #1。这会创建一个新的Activity #1实例,而非回到之前启动Activity #2的原实例,导致原实例的onActivityResult完全不会触发。

正确实现步骤

1. 修改Activity #2的返回按钮逻辑

删除手动启动Activity #1的代码,仅保留setResult和finish:

backButton.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        int currentPosition = getCurrentPosition(songName, songsList);
        String currentSongName = songsList.get(currentPosition);
        int currentSongId = getSongIdByName(currentSongName);
        Intent resultIntent = new Intent();
        resultIntent.putExtra("lastViewedSongId", currentSongId);
        resultIntent.putExtra("lastViewedSongName", currentSongName);
        setResult(RESULT_OK, resultIntent);
        finish(); // 直接finish,系统会自动回到之前的Activity #1实例
    }
});

2. Activity #1使用新版ActivityResultLauncher替代onActivityResult

由于onActivityResult已废弃,推荐使用registerForActivityResult接收返回数据:

// 在Activity #1中定义Launcher
private final ActivityResultLauncher<Intent> setListOpenViewLauncher = registerForActivityResult(
        new ActivityResultContracts.StartActivityForResult(),
        result -> {
            if (result.getResultCode() == Activity.RESULT_OK && result.getData() != null) {
                Intent data = result.getData();
                int lastViewedSongId = data.getIntExtra("lastViewedSongId", -1);
                String lastViewedSongName = data.getStringExtra("lastViewedSongName");
                int position = songNames.indexOf(lastViewedSongName);
                Log.d("SttList", "(SetListMasterViewActivity) Received lastViewedSongId: " + lastViewedSongId);
                Log.d("SttList", "(SetListMasterViewActivity) Received lastViewedSongName: " + lastViewedSongName);
                if (position != -1) {
                    TextView songTextView = (TextView) songsListView.getChildAt(position);
                    songTextView.setBackgroundColor(Color.YELLOW);
                }
            }
        }
);

// 启动Activity #2时使用这个Launcher
private void startSetListOpenViewActivity() {
    Intent intent = new Intent(this, SetListOpenViewActivity.class);
    // 传递需要的初始数据
    setListOpenViewLauncher.launch(intent);
}

3. 处理系统返回键的情况(可选)

若用户点击系统自带返回键,需在Activity #2的onBackPressed方法中也设置返回结果:

@Override
public void onBackPressed() {
    // 组装返回数据逻辑
    int currentPosition = getCurrentPosition(songName, songsList);
    String currentSongName = songsList.get(currentPosition);
    int currentSongId = getSongIdByName(currentSongName);
    Intent resultIntent = new Intent();
    resultIntent.putExtra("lastViewedSongId", currentSongId);
    resultIntent.putExtra("lastViewedSongName", currentSongName);
    setResult(RESULT_OK, resultIntent);
    super.onBackPressed(); // 调用父类方法完成finish
}

内容的提问来源于stack exchange,提问作者kiteandwindsurfer

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.28 20:55:35