一。旧版回调
startActivityForResult(intent, 3);//注意要求码
- //接收其它页面回传结果和显示图片
- @Override
- public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
- super.onActivityResult(requestCode, resultCode, data);
-
-
- //页面传回来的值
- if (requestCode == 1)
- if (data != null) {
- //处理代码在此地
- String dataCback = data.getStringExtra("EXTRA_Colors");
- ;// 得到子窗口ChildActivity的回传数据
- // edx_color.setText(dataCback);
-
- }
-
- //页面助用户传回来的值
- if (requestCode == 3)
- if (data != null) {
- //处理代码在此地
- String dataCback = data.getStringExtra("Milk_id");
- ;// 得到子窗口ChildActivity的回传数据
- String dataCbackspzxm = data.getStringExtra("milkusre_bame");
- ;// 得到子窗口ChildActivity的回传数据
- String dataCbackspzxmc = data.getStringExtra("Milk_name");
- ;// 得到子窗
- milkusre.setText(dataCbackspzxm);
- }
-
-
-
-
-
- }
换成,在启动执行
-
- //这个registerForActivityResult方法要写在Activity的onCreate方法里(在Activity创建的时候就要创建出来,不能等到使用的时候再创建,不然会报错LifecycleOwner com.xx.MainActivity@28b4e79 is attempting to register while current state is RESUMED. LifecycleOwners must call register before they are STARTED.)。
- ActivityResultLauncher
intentActivityResultLauncher = registerForActivityResult( - new ActivityResultContracts.StartActivityForResult(),
- result -> {
- /*
- if (result.getResultCode() == RESULT_OK) {
- //获取返回的结果
- String data = result.getData().getStringExtra("data");
- Toast.makeText(activity, data, Toast.LENGTH_SHORT).show();
- }
- */
-
-
- //页面传回来的值
- if (result.getResultCode() == 1){
- //获取返回的结果
- String data = result.getData().getStringExtra("data");
- if (data != null) {
- //处理代码在此地
- // String dataCback = data.getStringExtra("EXTRA_Colors");
- ;// 得到子窗口ChildActivity的回传数据
- // edx_color.setText(dataCback);
-
- }
- }
-
-
- //页面助用户传回来的值
- if (result.getResultCode() == 3){
- //获取返回的结果
- String dataCbackspzxm = result.getData().getStringExtra("milkusre_bame");
- if (dataCbackspzxm != null) {
- //处理代码在此地
-
- ;// 得到子窗口ChildActivity的回传数据
- // String dataCbackspzxm = data.getStringExtra("milkusre_bame");
-
- milkusre.setText(dataCbackspzxm);
- }
- }
-
-
-
-
-
-
-
- });