我有四个活动:

活动A

 private void addCard() {
        Intent intent = new Intent(MainActivity.this, GetNumberActivity.class);
        startActivityForResult(intent, REQUEST_CODE_CREATE);
    }

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == REQUEST_CODE_CREATE) {
        if (resultCode == RESULT_OK) {
            if (data.hasExtra("data")) {
                // Card has been create
                Toast.makeText(getApplication(), "Karata została wygenerowana.", Toast.LENGTH_SHORT).show();
            }
        }
    }
}


活动B

 Intent intent = new Intent(GetNumberActivity.this, ScanQrCodeActivity.class);
                intent.putExtra(EXTRA_MESSAGE, uunitValue);
                startActivityForResult(intent, REQUEST_CODE);


然后,在第二个活动中,我必须将数据传递给第三个活动。

活动C

    Card card = new Card(path3, base32, nameCard, intervalTotp, passwordHotp, getDate(), expirationDate, hotpValue);
                                    Intent intent = new Intent(ScanQrCodeActivity.this, Stage3Activity.class);
                                    intent.putExtra("card", card);
                                    startActivity(intent);
finish();


活动D

Intent data = new Intent(Stage3Activity.this,MainActivity.class);
                data.putExtra("data", card);
                startActivityForResult(data, RESULT_OK);


当我按活动A上的按钮时,不显示Toast。

最佳答案

您需要按照以下步骤更新代码

if (resultCode == RESULT_OK) {
        if (requestCode == REQUEST_CODE_CREATE) {
            if (data.hasExtra("data")) {
                // Card has been create
                Toast.makeText(getApplication(), "Karata została wygenerowana.", Toast.LENGTH_SHORT).show();
            }
        }
    }


首先检查RESULT_OK,然后继续进行

编码愉快!

08-18 05:46