一。旧版回调文章来源:https://www.toymoban.com/news/detail-728495.html
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);
}
}
换成,在启动执行文章来源地址https://www.toymoban.com/news/detail-728495.html
//这个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<Intent> 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);
}
}
});
到了这里,关于Android之startActivityForResult过时升级替换方法的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!