徐锦洪 发表于 2024-11-22 14:46:08

【Android Studio】ActivityResultLauncher

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:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
      android:id="@+id/pushtoA"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:gravity="center|center_horizontal"
      android:text="@string/page4"
      android:textSize="34sp" />

    <Button
      android:id="@+id/ButtonToA"
      android:layout_width="match_parent"
      android:layout_height="124dp"
      B_layout:android:gravity="center"
      android:text="@string/Button4"
      android:textSize="34sp" />

    <TextView
      android:id="@+id/viewB"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:gravity="center"
      android:text="@string/view2"
      android:textSize="34sp" />
</LinearLayout>
ActivityB_layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <TextView
      android:id="@+id/pushtoB"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:gravity="center|center_horizontal"
      android:text="@string/page3"
      android:textSize="34sp" />

    <Button
      android:id="@+id/ButtonToB"
      android:layout_width="match_parent"
      android:layout_height="124dp"
      android:gravity="center"
      android:text="@string/Button3"
      android:textSize="34sp" />

    <TextView
      android:id="@+id/viewA"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:gravity="center"
      android:text="@string/view1"
      android:textSize="34sp" />
</LinearLayout>
向下一个活动传递数据,通过Intent类,此处我们使用点击Button触发,在下一个页面显示信息。
新建Activity3页面,在Activity文件中创建一个Intent的对象,传入上下文,和下一个活动的.class
https://i-blog.csdnimg.cn/direct/f8cfb981902c43a7b8dbf3fb4ade075b.png
在Activity4页面,写入以下内容:
https://i-blog.csdnimg.cn/direct/1899fb0dd0604627a332a0eb4e6dcd7e.png
运行效果:
ActivityA界面:
https://i-blog.csdnimg.cn/direct/5575d9d9bf674bc18d918d3f958de0c0.png
点击button显示:
https://i-blog.csdnimg.cn/direct/19cda99e089c43e2b7238e558c65d1c5.png
显示成功!
3.2向上一个Activity传递数据

如何实现点击B的按钮再跳转A?并且传值使用launcher,因为如许A就能根据id号来精确判定分别来自于哪个页面的button值
(1)首先在ActivityA中设置ActivityResultLauncher:
首先,你需要在FirstActivity中定义一个ActivityResultLauncher。这通常是在你的Activity的onCreate方法中完成的。
// ActivityA.java
private ActivityResultLauncher<Intent> someActivityResultLauncher = registerForActivityResult(
    new ActivityResultContracts.StartActivityForResult(),
    new ActivityResult.Callback<ActivityResult>() {
      @Override
      public void onActivityResult(ActivityResult result) {
            if (result.getResultCode() == Activity.RESULT_OK) {
                Intent data = result.getData();
                if (data != null) {
                  String returnedData = data.getStringExtra("resultKey");
                  // 处理从SecondActivity返回的数据
                }
            }
      }
    }
);
如图:
https://i-blog.csdnimg.cn/direct/b4a9ae503462410eb632d643fead2f96.png
   [!NOTE]

[*]留意:ActivityResultContracts.StartActivityForResult()是一个预定义的合同,用于启动一个Activity并期待效果。
(2)在FirstActivity中启动SecondActivity:使用你定义的ActivityResultLauncher来启动SecondActivity。
https://i-blog.csdnimg.cn/direct/9f8cec73775b484f9644838727cfb8f6.png
(3)在SecondActivity中设置效果数据并返回:
在SecondActivity中,当用户点击按钮时,你需要创建一个Intent来携带返回数据,并调用setResult方法来竣事SecondActivity并返回数据。
// ActivityB.java
Button returnButton = findViewById(R.id.return_button);
returnButton.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
      Intent returnIntent = new Intent();
      returnIntent.putExtra("resultKey", "这是从SecondActivity传递回的数据");
      setResult(Activity.RESULT_OK, returnIntent);
      finish(); // 结束SecondActivity
    }
});
按照以上格式写我们自己的代码如下:
https://i-blog.csdnimg.cn/direct/1e7ea0a09c1d44cdadf2cbba261866bd.png
运行效果(从B点击按钮跳转A)
https://i-blog.csdnimg.cn/direct/d7a3c28a0663433296dbe096ff7d23b7.png
跳转成功!
如今,当用户从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企服之家,中国第一个企服评测及商务社交产业平台。
页: [1]
查看完整版本: 【Android Studio】ActivityResultLauncher