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

查看:175
本文介绍了与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,我需要执行网络请求.我不想复制对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.

推荐答案

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和Navigation中的片段,并确保仅在至少STARTED时才将结果发送到您的Fragment. ( b/149787344 )

FragmentManager获得了两种新方法:

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