Home Activity Result API使用
Post
Cancel

Activity Result API使用

简介

Activity Result API 提供了用于注册结果、启动结果以及在系统分派结果后对其进行处理的组件。是在AndroidX中被引用,并被强烈推荐的组件。

使用

在activity和fragment中都增加了一个registerForActivityResult方法来注册约定协议以及返回结果

1
2
3
4
5
6
7
8
@MainThread
@NonNull
@Override
public final <I, O> ActivityResultLauncher<I> registerForActivityResult(
        @NonNull final ActivityResultContract<I, O> contract,
        @NonNull final ActivityResultCallback<O> callback) {
    return prepareCallInternal();
}

协议class

1
2
3
4
5
6
7
8
9
10
// * @param <I> input type
// * @param <O> output type
public abstract class ActivityResultContract<I, O> {

    public abstract @NonNull Intent createIntent(@NonNull Context context, @SuppressLint("UnknownNullness") I input);

    @SuppressLint("UnknownNullness")
    public abstract O parseResult(int resultCode, @Nullable Intent intent);
    
}

默认协议

系统中默认实现了比较多的协议来提供给开发者直接使用

大概列了前面的几个在这里,还默认有部分都是在ActivityResultContracts中的,需要再补充

This post is licensed under CC BY 4.0 by the author.