Android Studio - Java - 按后退后父活动中未收到子活动的意图

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

我的

SetListEditAdapter
类中的这个 Intent 启动了我的父类
SetListMasterViewActivity
:

    private void openSetListDetails(int setListId) {
        if (openSetListMode == OpenSetListMode.OPEN) {
            Intent openSetListIntent;
            openSetListIntent = new Intent(context, SetListMasterViewActivity.class);
            openSetListIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP); // Add the flag here
            openSetListIntent.putExtra("SET_LIST_ID", setListId);
            context.startActivity(openSetListIntent);
        } else {
            Intent intent;
            intent = new Intent(context, SetListDetailsActivity.class);
            intent.putExtra("SET_LIST_ID", setListId);
            context.startActivity(intent);
        }
        Log.d("SttList", "(SetListEditAdapter) line #59 - setListId: " + setListId);
        Log.d("SttList", "(SetListEditAdapter) line #61 - SetListMasterViewActivity launched with setListId: " + setListId);
    }

在新打开的

parent
活动 (
SetListMasterViewActivity
) 中,我启动子活动 (
SetListOpenViewActivity
): Intent openViewIntent = new Intent(SetListMasterViewActivity.this, SetListOpenViewActivity.class);

            // Pass necessary data with the intent
            openViewIntent.putExtra("songId", selectedSong.getSongId());
            openViewIntent.putExtra("songName", selectedSong.getSongName());
            openViewIntent.putExtra("setId", setListId); // Assuming you need to pass the setListId
            openViewIntent.putExtra("songsList", new ArrayList<>(songsList)); // Convert List to ArrayList before passing

            // Start the activity
            openViewIntent.setFlags(Intent.FLAG_ACTIVITY_FORWARD_RESULT);
            startActivity(openViewIntent);

我在

child
活动 (
SetListOpenViewActivity
) 中毫无问题地收到意图:

        Intent intent = getIntent();
        songId = intent.getLongExtra("songId", 0);
        songName = intent.getStringExtra("songName");
        setId = intent.getIntExtra("setId", 0);
        dbHelper = new DatabaseHelper(this);
        songsList = intent.getParcelableArrayListExtra("songsList");

当按下

child
活动中的“返回”按钮时,处理将返回到
parent
活动,并具有以下意图:

                Intent resultIntent = new Intent();
                resultIntent.putExtra("lastViewedSongId", currentSongId);
                resultIntent.putExtra("lastViewedSongName", currentSongName);
                resultIntent.putExtra("backButtonClicked", true); // Include backButtonClicked flag
                setResult(RESULT_OK, resultIntent);
                Log.d("SttList", "(SetListOpenViewActivity) - line #120 - Intent contents: " + getIntent().getExtras());
                Log.d("SttList", "(SetListOpenViewActivity) - line #121 - Setting result intent: " + resultIntent);
                finish(); 

parent
活动中,我希望收到
child's
意图:

    @Override
    protected void onResume() {
        super.onResume();
        Log.d("SttList", "(SetListMasterViewActivity) - line #254 - onResume method start");
        // Check if the Intent contains the backButtonClicked flag
        Intent intent = getIntent();
        Log.d("SttList", "(SetListMasterViewActivity) - line #257 - Intent received: " + getIntent().getExtras());
        if (intent != null && intent.hasExtra("backButtonClicked")) {
            boolean backButtonClicked = intent.getBooleanExtra("backButtonClicked", false);
            Log.d("SttList", "(SetListMasterViewActivity) - line #260 - backButtonClicked: " + backButtonClicked);
            if (backButtonClicked) {
                // Handle the back button click
                Log.d("SttList", "(SetListMasterViewActivity) - line #263 - Back button clicked in child activity");

                // Process the backButtonClicked flag here or call onActivityResult if needed
                // ...
            }
        }
    }

但是 Log.d 输出引用正在启动

parent
活动的 Intent:

 D  (SetListMasterViewActivity) - line #257 - Intent received: Bundle[{SET_LIST_ID=280}]

我已经尝试过:

  1. 使用
    onActivityResult
    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        Log.d("SttList", "(SetListMasterViewActivity) - line #216 - Received intent: " + data);
        Log.d("SttList", "(SetListMasterViewActivity) line #217 - requestCode: " + requestCode + ", resultCode: " + resultCode);
        if (resultCode == RESULT_OK && data != null) {
...

然而,所有

Log.d
语句均未达成。

  1. 尝试将其添加到
    child activity
    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
    }

但是,在

parent
中,没有收到任何
child's
Intents

java android android-studio android-intent parent-child
1个回答
0
投票

根据文档方法Activity.getIntent()

返回启动此活动的意图。

通过调用

finish()
,您并不是有意启动一个新活动(就像
startActivity(intent)
那样。它只是销毁该活动并从后堆栈中弹出前一个活动。

为了从子活动返回结果意图,您需要使用

startActivityForResult()
启动它并在
onActivityResult()
中等待数据。但当
Activity Result API
出现后,现代的方式发生了一些变化。从指南开始。

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