startActivityForResult の代替メソッドは直接コピーでき、現在のアクティビティが別のアクティビティにジャンプするときに、受信したデータが渡されます。

アクティビティを開始する

    // 注册监听
    ActivityResultLauncher<Intent> intentActivityResultLauncher = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(), new ActivityResultCallback<ActivityResult>() {
        @Override
        public void onActivityResult(ActivityResult result) {
            //此处是跳转的result回调方法
            Log.d(TAG, "onActivityResult");
            if (result.getData() != null && result.getResultCode() == Activity.RESULT_OK) {
                // 数据在此处理
                endfloor = result.getData().getStringExtra("targetFloor").toUpperCase();
                Log.d(TAG, "onActivityResult endfloor:"+endfloor);
                }
            } else {
                Toast.makeText(IpttActivity.this, "配置数据返回失败", Toast.LENGTH_SHORT).show();
            }
        }
    });
    
     // 启动另一个activity并发送数据
     Intent intent = new Intent(this, ConfActivity.class);
     intent.putExtra("endLatitude", endLatitude);
     intent.putExtra("endLongitude", endLongitude);
     intent.putExtra("endPoi", endPoi);
     intentActivityResultLauncher.launch(intent);

別のアクティビティは、ボタンをクリックするとすぐに前のアクティビティに戻り、データを送り返します。

    @Override
    public void onClick(View v) {
        mTargetFloor = "mTargetFloor";
        setResult(Activity.RESULT_OK, new Intent().putExtra("targetFloor", mTargetFloor));
        finish();
    }

おすすめ

転載: blog.csdn.net/weixin_44440669/article/details/124757270
おすすめ