diff --git a/docs-source/src/en/api/special-features/xposed-channel.md b/docs-source/src/en/api/special-features/xposed-channel.md index 08e125ec..fd2ee6fb 100644 --- a/docs-source/src/en/api/special-features/xposed-channel.md +++ b/docs-source/src/en/api/special-features/xposed-channel.md @@ -176,7 +176,7 @@ The `key` of callback event C is not duplicated with others. Although the `key` of callback event D is the same as that of callback event C, their Host Apps are different, so they can exist at the same time. -Callback event E is in another Activity, although the `key` of callback event F and callback event E is the same, but they are not the same Host App, so they can exist at the same time. +Callback event E is in another **Activity**, although the `key` of callback event F and callback event E is the same, but they are not the same Host App, so they can exist at the same time. In summary, the final callback events B, C, D, E, and F can all be created successfully. @@ -198,6 +198,65 @@ If you want to use **dataChannel** in **Fragment**, use **activity?.dataChannel( ::: +If you want to manually set the response priority (condition) of each callback event in the same **Activity**, you can use `ChannelPriority`. + +For example, if you are using one **Activity** binding multiple **Fragment** cases, this will be able to solve this problem. + +> The following example + +```kotlin +open class BaseFragment : Fragment() { + + /** Identify that the current Fragment is in the onResume lifecycle */ + var isResume = false + + override fun onResume() { + super. onResume() + isResume = true + } + + override fun onPause() { + super. onPause() + isResume = false + } + + override fun onStop() { + super. onStop() + isResume = false + } +} + +class FragmentA : BaseFragment() { + + // Omit part of initialization code + //... + + override fun onViewCreated(view: View, savedInstanceState: Bundle?) { + // Use the custom isResume combined with the isVisible condition + // To judge that the current Fragment is in the displayed state + activity?.dataChannel(packageName = "com.example.demo1") + ?.wait(key = "test_key", ChannelPriority { isResume && isVisible }) { + // Your code here. + } + } +} + +class FragmentB : BaseFragment() { + + // Omit part of initialization code + //... + + override fun onViewCreated(view: View, savedInstanceState: Bundle?) { + // Use the custom isResume combined with the isVisible condition + // To judge that the current Fragment is in the displayed state + activity?.dataChannel(packageName = "com.example.demo2") + ?.wait(key = "test_key", ChannelPriority { isResume && isVisible }) { + // Your code here. + } + } +} +``` + ## Security Instructions In the Module environment, you can only receive the communication data sent by **the Host App of the specified package name** and can only send to **the Host App of the specified package name**, except for System Framework. diff --git a/docs-source/src/zh-cn/api/special-features/xposed-channel.md b/docs-source/src/zh-cn/api/special-features/xposed-channel.md index 97ba1123..8d960b3c 100644 --- a/docs-source/src/zh-cn/api/special-features/xposed-channel.md +++ b/docs-source/src/zh-cn/api/special-features/xposed-channel.md @@ -170,7 +170,7 @@ class OtherActivity : AppCompatActivity() { 回调事件 C 的 `key` 不与其它重复,虽然回调事件 D 的 `key` 与回调事件 C 相同,但是它们的宿主不同,所以可以同时存在。 -回调事件 E 在另一个 Activity 中,回调事件 F 与回调事件 E 的 `key` 虽然相同,但它们也不是同一个宿主,所以可以同时存在。 +回调事件 E 在另一个 **Activity** 中,回调事件 F 与回调事件 E 的 `key` 虽然相同,但它们也不是同一个宿主,所以可以同时存在。 综上所述,最终回调事件 B、C、D、E、F 都可被创建成功。 @@ -192,6 +192,63 @@ class OtherActivity : AppCompatActivity() { ::: +如果你希望在同一个 **Activity** 中手动设置每个回调事件的响应优先级 (条件),你可以使用 `ChannelPriority`。 + +例如,你正在使用一个 **Activity** 绑定多个 **Fragment** 的情况,这将能够解决这个问题。 + +> 示例如下 + +```kotlin +open class BaseFragment : Fragment() { + + /** 标识当前 Fragment 处于 onResume 生命周期 */ + var isResume = false + + override fun onResume() { + super.onResume() + isResume = true + } + + override fun onPause() { + super.onPause() + isResume = false + } + + override fun onStop() { + super.onStop() + isResume = false + } +} + +class FragmentA : BaseFragment() { + + // 省略部分装载代码 + // ... + + override fun onViewCreated(view: View, savedInstanceState: Bundle?) { + // 使用自定义的 isResume 结合 isVisible 条件判断当前 Fragment 正处于显示状态 + activity?.dataChannel(packageName = "com.example.demo1") + ?.wait(key = "test_key", ChannelPriority { isResume && isVisible }) { + // Your code here. + } + } +} + +class FragmentB : BaseFragment() { + + // 省略部分装载代码 + // ... + + override fun onViewCreated(view: View, savedInstanceState: Bundle?) { + // 使用自定义的 isResume 结合 isVisible 条件判断当前 Fragment 正处于显示状态 + activity?.dataChannel(packageName = "com.example.demo2") + ?.wait(key = "test_key", ChannelPriority { isResume && isVisible }) { + // Your code here. + } + } +} +``` + ## 安全性说明 在模块环境中,你只能接收**指定包名的宿主**发送的通讯数据且只能发送给**指定包名的宿主**,系统框架除外。