【Android Studio】ActivityResultLauncher

打印 上一主题 下一主题

主题 831|帖子 831|积分 2493


title: Android Studio——ActivityResultLauncher

【Android Studio】ActivityResultLauncher


  
一:什么是ActivityResultLauncher

ActivityResultLauncher是Android Studio用于在应用程序中启动活动并吸收其效果的组件,它是startActivityForResult的改进版本。
优点:


  • 类型安全:效果处置惩罚逻辑和启动器绑定在一起,避免传统方法中可能出现的类型转换错误。
  • 解耦:不需要重写onActivityResult(),逻辑更加清晰。
  • 生命周期感知:启动器与生命周期相关联,在适当的时间注册和取消注册。
二:ActivityResultLauncher

ActivityResultLauncher 是一个用于简化 Android 开发中 startActivityForResult 流程的新 API。它通过注册 contract 和 ActivityResultCallback 来启动和吸收活动效果,提供更直观的流程管理。ActivityResultLauncher 是 Android 官方推荐的用来替代 startActivityForResult 的新方式,通过它可以非常方便地调用系统 Intent 进行拍照、选取本地文件等操纵。
三:活动之间互相传递数据

【目的】创建两个Activity文件实现点击按钮互相在页面上显示数据。
3.1向下一个Activity传递数据

前置要求:
设置layout的结构:
ActivityA_layout:
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.     android:orientation="vertical"
  4.     android:layout_width="match_parent"
  5.     android:layout_height="match_parent">
  6.     <TextView
  7.         android:id="@+id/pushtoA"
  8.         android:layout_width="match_parent"
  9.         android:layout_height="wrap_content"
  10.         android:gravity="center|center_horizontal"
  11.         android:text="@string/page4"
  12.         android:textSize="34sp" />
  13.     <Button
  14.         android:id="@+id/ButtonToA"
  15.         android:layout_width="match_parent"
  16.         android:layout_height="124dp"
  17.       B_layout:  android:gravity="center"
  18.         android:text="@string/Button4"
  19.         android:textSize="34sp" />
  20.     <TextView
  21.         android:id="@+id/viewB"
  22.         android:layout_width="match_parent"
  23.         android:layout_height="wrap_content"
  24.         android:gravity="center"
  25.         android:text="@string/view2"
  26.         android:textSize="34sp" />
  27. </LinearLayout>
复制代码
ActivityB_layout:
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.     android:layout_width="match_parent"
  4.     android:layout_height="match_parent"
  5.     android:orientation="vertical">
  6.     <TextView
  7.         android:id="@+id/pushtoB"
  8.         android:layout_width="match_parent"
  9.         android:layout_height="wrap_content"
  10.         android:gravity="center|center_horizontal"
  11.         android:text="@string/page3"
  12.         android:textSize="34sp" />
  13.     <Button
  14.         android:id="@+id/ButtonToB"
  15.         android:layout_width="match_parent"
  16.         android:layout_height="124dp"
  17.         android:gravity="center"
  18.         android:text="@string/Button3"
  19.         android:textSize="34sp" />
  20.     <TextView
  21.         android:id="@+id/viewA"
  22.         android:layout_width="match_parent"
  23.         android:layout_height="wrap_content"
  24.         android:gravity="center"
  25.         android:text="@string/view1"
  26.         android:textSize="34sp" />
  27. </LinearLayout>
复制代码
向下一个活动传递数据,通过Intent类,此处我们使用点击Button触发,在下一个页面显示信息。
新建Activity3页面,在Activity文件中创建一个Intent的对象,传入上下文,和下一个活动的.class

在Activity4页面,写入以下内容:

运行效果:
ActivityA界面:

点击button显示:

显示成功!
3.2向上一个Activity传递数据

如何实现点击B的按钮再跳转A?并且传值使用launcher,因为如许A就能根据id号来精确判定分别来自于哪个页面的button值
(1)首先在ActivityA中设置ActivityResultLauncher:
首先,你需要在FirstActivity中定义一个ActivityResultLauncher。这通常是在你的Activity的onCreate方法中完成的。
  1. // ActivityA.java  
  2. private ActivityResultLauncher<Intent> someActivityResultLauncher = registerForActivityResult(  
  3.     new ActivityResultContracts.StartActivityForResult(),  
  4.     new ActivityResult.Callback<ActivityResult>() {  
  5.         @Override  
  6.         public void onActivityResult(ActivityResult result) {  
  7.             if (result.getResultCode() == Activity.RESULT_OK) {  
  8.                 Intent data = result.getData();  
  9.                 if (data != null) {  
  10.                     String returnedData = data.getStringExtra("resultKey");  
  11.                     // 处理从SecondActivity返回的数据  
  12.                 }  
  13.             }  
  14.         }  
  15.     }  
  16. );
复制代码
如图:

   [!NOTE]
  

  • 留意:ActivityResultContracts.StartActivityForResult()是一个预定义的合同,用于启动一个Activity并期待效果。
  (2)在FirstActivity中启动SecondActivity:使用你定义的ActivityResultLauncher来启动SecondActivity。

(3)在SecondActivity中设置效果数据并返回
在SecondActivity中,当用户点击按钮时,你需要创建一个Intent来携带返回数据,并调用setResult方法来竣事SecondActivity并返回数据。
  1. // ActivityB.java  
  2. Button returnButton = findViewById(R.id.return_button);  
  3. returnButton.setOnClickListener(new View.OnClickListener() {  
  4.     @Override  
  5.     public void onClick(View v) {  
  6.         Intent returnIntent = new Intent();  
  7.         returnIntent.putExtra("resultKey", "这是从SecondActivity传递回的数据");  
  8.         setResult(Activity.RESULT_OK, returnIntent);  
  9.         finish(); // 结束SecondActivity  
  10.     }  
  11. });
复制代码
按照以上格式写我们自己的代码如下:

运行效果(从B点击按钮跳转A)

跳转成功!
如今,当用户从SecondActivity点击按钮时,SecondActivity会竣事,并且携带的数据会通过ActivityResultLauncher的回调方法返回给FirstActivity。
四:总结

这种方法的好处是,它使用了Android Jetpack中的ActivityResultAPIs,这些API旨在简化Activity之间的效果传递,并处置惩罚一些与请求代码和效果代码相关的常见错误。此外,使用ActivityResultLauncher还可以避免内存泄漏,因为它不需要你在Activity中显式地覆盖onActivityResult方法。
最后,推荐一个关于ActivityResultlauncher深入学校的博客:
pack中的ActivityResultAPIs,这些API旨在简化Activity之间的效果传递,并处置惩罚一些与请求代码和效果代码相关的常见错误。此外,使用ActivityResultLauncher还可以避免内存泄漏,因为它不需要你在Activity中显式地覆盖onActivityResult方法。
最后,推荐一个关于ActivityResultlauncher深入学习的博客:
https://github.com/SheTieJun/BaseKit/wiki/ActivityResultLauncher%E4%BD%BF%E7%94%A8
感谢观看!

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

徐锦洪

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表