与 Android 架构导航等效的 startActivityForResult() [英] Equivalent of startActivityForResult() with Android Architecture Navigation

查看:23
本文介绍了与 Android 架构导航等效的 startActivityForResult()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含 3 个屏幕的工作流程.从屏幕 1"到访问屏幕 2",用户必须接受某种条款和条件,我在我的图片中称之为模态".但他只需要接受一次那些条件.下次他在第一个屏幕时,他可以直接进入屏幕2.用户可以选择不接受条款,因此我们回到屏幕1"而不尝试去屏幕2".

I have a workflow with 3 screens. From "screen 1" to access to "screen 2", the user must accept some kind of terms and conditions that I call in my picture "modal". But he only has to accept those conditions once. The next time he is on the first screen, he can go directly to screen 2. The user can chose to NOT accept the terms, and therefore we go back to "screen 1" and do not try to go to "screen 2".

我想知道如何使用新的导航组件.

I am wondering how to do it with the new navigation component.

以前,我会怎么做:

  • 在屏幕 1 上,检查用户是否必须接受条件
  • 如果不是,则启动屏幕 2"活动
  • 如果是,使用 startActivityForResult() 并等待模态的结果.将条款标记为已接受.启动屏幕 2"
  • On screen 1, check if the user must accept the conditions
  • If no, start "screen 2" activity
  • If yes, use startActivityForResult() and wait result from the modal. Mark the terms as accepted. Start "screen 2"

但是有了导航图,就没有办法启动一个Fragment来获取结果.

But with the navigation graph, there is no way to start a Fragment to obtain a result.

我可以在模态"屏幕中将条款标记为已接受,然后从那里开始屏幕 2".问题是要访问屏幕 2,我需要进行网络请求.我不想在屏幕 1"和模态"中复制对 API 的调用并处理其结果.

I could mark the terms as accepted in the "modal" screen and start the "screen 2" from there. The thing is that to access to the screen 2, I need to do a network request. I do not want to duplicate the call to the API and processing its outcome in both "screen 1" and "modal".

有没有办法使用 Jetpack 导航从模态"返回到屏幕 1"并提供一些信息(用户接受条款)?

我目前通过使用 Yahya 在下面建议的相同流程来解决它:仅将 Activity 用于模式并使用屏幕 1"中的 startActivityForResult.我只是想知道我是否可以在整个流程中继续使用导航图.

I currently get around it by using the same flow that Yahya suggests below: using an Activity just for the modal and using startActivityForResult from the "screen 1". I am just wondering if I could continue to use the navigation graph for the whole flow.

推荐答案

1.3.0-alpha04 版本的 AndroidX Fragment 库,他们引入了允许在 Fragment 之间传递数据的新 API.

In the 1.3.0-alpha04 version of AndroidX Fragment library they introduced new APIs that allow passing data between Fragments.

添加了通过 FragmentManager 上的新 API 在两个 Fragment 之间传递结果的支持.这适用于导航中的层次结构片段(父/子)、DialogFragments 和片段,并确保结果仅在至少 STARTED 时发送到您的 Fragment.(b/149787344)

Added support for passing results between two Fragments via new APIs on FragmentManager. This works for hierarchy fragments (parent/child), DialogFragments, and fragments in Navigation and ensures that results are only sent to your Fragment while it is at least STARTED. (b/149787344)

FragmentManager 获得了两个新方法:

如何使用?

FragmentA中,在onCreate方法的FragmentManager中添加FragmentResultListener:

In FragmentA add FragmentResultListener to the FragmentManager in the onCreate method:

setFragmentResultListener("request_key") { requestKey: String, bundle: Bundle ->
    val result = bundle.getString("your_data_key")
    // do something with the result
}

FragmentB 中添加此代码以返回结果:

In FragmentB add this code to return the result:

val result = Bundle().apply {
    putString("your_data_key", "Hello!")
}
setFragmentResult("request_key", result)

启动 FragmentB 例如:通过使用:

Start FragmentB e.g.: by using:

findNavController().navigate(NavGraphDirections.yourActionToFragmentB())

要关闭/完成 FragmentB 调用:

To close/finish FragmentB call:

findNavController().navigateUp()

现在,您的 FragmentResultListener 应该会收到通知,您应该会收到结果.

Now, your FragmentResultListener should be notified and you should receive your result.

(我使用 fragment-ktx 来简化上面的代码)

(I'm using fragment-ktx to simplify the code above)

这篇关于与 Android 架构导航等效的 startActivityForResult()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆