关于 registerForActivityResult()的使用方法,不能说详细,只能说略懂得例子
创始人
2024-03-21 10:09:28
0次

目录

1.情况说明

2.registerForActivityResult()的使用方法


1.情况说明

startActivityForResult();函数过时

使用了

 registerForActivityResult()进行了代替

2.registerForActivityResult()的使用方法

数据来源

(2条消息) registerForActivityResult()的使用方法例子_发狂先生的博客-CSDN博客_registerforactivityresult用法

registerForActivityResult(ActivityResultContracts.TakeVideo())始终为空 - 我爱学习网 (5axxw.com)

registerForActivityResult 解决 startActivityForResult(Intent!, Int): Unit is deprecated. Deprecated in Java - Android - 大象笔记 (sunzhongwei.com)

https://blog.csdn.net/weixin_44618862/article/details/98209369

过时的OnActivityResult替代品-registerForActivityResult源码解析 - 未知用户的博客 (7449.github.io)

其中最为详细的在

(2条消息) registerForActivityResult()的使用方法例子_发狂先生的博客-CSDN博客_registerforactivityresult用法

registerForActivityResult 解决 startActivityForResult(Intent!, Int): Unit is deprecated. Deprecated in Java - Android - 大象笔记 (sunzhongwei.com)

3.例子

数据来源

(2条消息) registerForActivityResult()的使用方法例子_发狂先生的博客-CSDN博客_registerforactivityresult用法

然后在Studio编写如下(自己加了一些)

 

在第一个activity中

在1中得java

package com.example.test1;import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.appcompat.app.AppCompatActivity;import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;public class ChuangShuFrist extends AppCompatActivity {TextView text_message;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.chuangshufrist);text_message = (TextView) findViewById(R.id.cshi1);Button button_transfer1 = findViewById(R.id.cshibu11);//按钮1ActivityResultLauncher requestDataLauncher = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(),result -> {Log.d("FirstActivity", result.getData().getStringExtra("data_return"));//tag表示标签信息,第二个参数表示要打印的信息,在这里表示可以在日志中看到该方法的执行});button_transfer1.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {Intent intent=new Intent(ChuangShuFrist.this,ChuangShuSecond.class);requestDataLauncher.launch(intent);//用的是requestDataLauncher}});}}

在1得xml中



如下图

在第二个activity中

在2中得java

package com.example.test1;import androidx.appcompat.app.AppCompatActivity;import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;public class ChuangShuSecond extends AppCompatActivity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.chuangshusecond);Button button3=findViewById(R.id.but2);button3.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {Intent intent=new Intent();intent.putExtra("data_return","Hello FirstActivity");setResult(RESULT_OK,intent);finish();}});}
}

在2得xml中



如下图

验证

QQ录屏20221206140057

验证第二次在 textview界面中,显示数据

 

相关内容

热门资讯

前端-session、jwt 目录:   (1)session (2&#x...
linux入门---制作进度条 了解缓冲区 我们首先来看看下面的操作: 我们首先创建了一个文件并在这个文件里面添加了...
关于测试,我发现了哪些新大陆 关于测试 平常也只是听说过一些关于测试的术语,但并没有使用过测试工具。偶然看到编程老师...
前缀和与对数器与二分法 1. 前缀和 假设有一个数组,我们想大量频繁的去访问L到R这个区间的和,...
nodejs:本地安装nvm实... 一、背景-使用不同版本node的原因 vue3+ts、nuxt3版本,node...
JAVA集合知识整理 Java集合知识整理 HashMap相关 HashMap的底层数据结构:jdk1.8之...
无刷直流电机介绍及单片机控制实... 无刷直流电机介绍及单片机控制实例前言基本概念优势与劣势使用寿命基本结构使用单片机控制实例电子调速器&...
fwdiary(2) dp2 1.传纸条  AcWing 275. 传纸条 - AcWing 走两条路,走一条最大的...
常用的DOS命令 常用的DOS命令 DOS(Disk Operating System,磁...
<C++> 类和对象(下) 1.const成员函数将const修饰的“成员函数”称之为const成员函数,cons...