The alternative method of startActivityForResult can be copied directly. When the current activity jumps to another activity, the received data is passed in

start activity

    // 注册监听
    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);

Another activity, after a button is clicked, directly returns to the previous activity and sends data back

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

Guess you like

Origin blog.csdn.net/weixin_44440669/article/details/124757270