Android Studio、java、onActivityResult 未使用 Intent 触发

问题描述 投票:0回答:1

我有两项活动:

活动#1

SetListMasterViewActivity

活动#2

SetListOpenViewActivity

Activity #1 最初使用 Intent 变量调用

Activity #2
,效果很好。 Activity #1 包含
List
歌曲,当用户单击列表中的任何歌曲时,该歌曲将打开并显示 Activity #2 中的所有歌词。通过在 Activity #2 中按返回,用户将再次返回到 Activity #1 中的列表,但这次之前在 Activity #2 中查看的歌曲现在突出显示(在 Activity #1 内的列表中)。

当用户从

Activity #2
单击 "Back" 时 - 将处理发送回 Activity #1 - 我遇到了问题。逻辑是,在Activity #2中,用户正在查看歌曲的文本(歌词)。当用户单击
"Back"
按钮时,处理将返回到 Activity #1,用户在 Activity #2 中查看的歌曲现在在列表中突出显示。

Activity #2

onCreate()
中,这里是我设置 Intent 的
"Back"
的代码:

        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();
            }
        });

启动

Intent
后,继续处理此方法:

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

哪个调用此方法:

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

活动#1中,这是接收

Intent
的地方:

    @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) {
            // Retrieve the last viewed song information from the Intent
            int lastViewedSongId = data.getIntExtra("lastViewedSongId", -1);
            String lastViewedSongName = data.getStringExtra("lastViewedSongName");

            // Find the position of the last viewed song in list of song names
            int position = songNames.indexOf(lastViewedSongName);
            // Log the received data
            Log.d("SttList", "(SetListMasterViewActivity) line #201 - Received lastViewedSongId: " + lastViewedSongId);
            Log.d("SttList", "(SetListMasterViewActivity) line #202 - Received lastViewedSongName: " + lastViewedSongName);

            // Highlight the song
            if (position != -1) {
                // Highlight the song at the found position
                TextView songTextView = (TextView) songsListView.getChildAt(position);
                songTextView.setBackgroundColor(Color.YELLOW);
            }
        }
    }

发生的情况是,Activity #1 打开,但没有收到来自 Activity #2 的 Intent。

onActivityResult
中的所有Log.d语句都没有运行,所以看起来
onActivityResult
被一起跳过了。

我尝试将

onActivityResult
中的所有代码移至
onResume
- 在 Activity #1 中 - 但发现
Intent
未在
onResume
内收到。我查看了发布的其他问题,但找不到我面临的问题。

我还尝试过什么:

  • setResult()
    是您需要它调用
    onActivityResult()
    - 是的,我正在使用
    setResult()
  • setResult
    我正在使用
    setResult(RESULT_OK, resultIntent);
  • 最初我尝试用
    startActivityForResult
    来编码意图,很多旧的答案都使用它,但是 AS 指出了这一点:
    'startActivityForResult(android.content.Intent, int)' is deprecated
java android android-intent onactivityresult
1个回答
0
投票

父活动(活动#1)将数据传递给子活动(活动#2),然后子活动将数据传递回父活动。

重要的是,Activity Result API 仅用于返回结果 - 将子 Activity 的结果传递回父 Activity。

这意味着父活动负责使用适当的合约调用

registerForActivityResult
。它将包含子活动的信息,作为您启动的
Intent
的一部分。

一旦子 Activity 启动,它就可以访问通过 Intent extra 传递给它的信息,并调用

setResult()
发回信息 - 调用
finish()
返回父 Activity。

只有当您返回父 Activity 时,附加到该

result ->
回调的
registerForActivityResult
lambda 回调才会被触发,并返回结果。

根本不覆盖

onActivityResult
。您根本打电话给
startActivityForResult
registerForActivityResult
并在返回的
launch
上调用
ActivityResultLauncher
是您在父活动方面实际需要做的所有事情。

© www.soinside.com 2019 - 2024. All rights reserved.