Forward and reverse value transfer in Android development

Forward and reverse value transfer in Android development

First definition, pay attention to the head

 String fxstr="矢量地图";

    //1.反向传值
    ActivityResultLauncher<Intent> launcher = registerForActivityResult(
            new ActivityResultContracts.StartActivityForResult(),
            result -> {
                if (result.getResultCode() == RESULT_OK) {
                    //拿到返回的数据
                     fxstr = result.getData().getStringExtra("data");
                    Toast.makeText(MainActivity.this, fxstr, Toast.LENGTH_SHORT).show();
                }
            });

Jump

  //反向传值2
     // bt_lay = (ImageView) findViewById(R.id.sa_lay);//2
      findViewById(R.id.sa_lay).setOnClickListener(view -> {
          //使用
          Intent intent2 = new Intent(this,MapLaysetActivity.class);
          intent2.putExtra("name",fxstr);
          launcher.launch(intent2);
      });

return


void  fxvalue(){

    //请求返回结果
    ActivityResultLauncher<Intent> launcher = registerForActivityResult(
            new ActivityResultContracts.StartActivityForResult(),
            result -> {
                if (result.getResultCode() == RESULT_OK) {
                    //拿到返回的数据
                    String str = result.getData().getStringExtra("data");
                    Toast.makeText(MainActivity.this, str, Toast.LENGTH_SHORT).show();
                }
            });

Second page

//Get the intent object 
intent = getIntent(); 
name = intent.getStringExtra("name");

============

name= radbtn.getText()+"";

Intent intent = new Intent();
intent.putExtra("data", name);
setResult(RESULT_OK, intent);
finish();

Guess you like

Origin blog.csdn.net/wushijun5200/article/details/129890168