问题描述
我有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,我需要执行网络请求.我不想复制对API的调用并在屏幕1"和模式"中处理其结果.
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库引入了新的API,这些API允许在Fragment
之间传递数据.
In the 1.3.0-alpha04 version of AndroidX Fragment library they introduced new APIs that allow passing data between Fragment
s.
FragmentManager
获得了两种新方法:
-
FragmentManager#setFragmentResult(String, Bundle)
,您可以像对待现有的Activity#setResult
; -
FragmentManager#setFragmentResultListener(String, LifecycleOwner, FragmentResultListener)
,它使您可以侦听/观察结果更改.
如何使用它?
在FragmentA
中的onCreate
方法中,将FragmentResultListener
添加到FragmentManager
:
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
通话:
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()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!