- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
在我的应用程序中,我使用具有多个片段架构的单个 Activity ,并使用导航库在它们之间导航。在其中一个片段中,我有多个类别,每个类别都与一个 ID 相关联。当点击一个类别时,我会使用下面的代码将用户带到相应的类别解释器屏幕。
val directions = MainNavGraphDirections.launchFragmentWithThisCategoryId(categoryId!!)
onRoute(AppRoute.Directions(directions))
categoryId
关联的解释器屏幕。 .一切都很好,直到现在,正确的解释器屏幕基于
categoryId
启动。 .在这个解释器屏幕中,我有一个带有标签
chatbot://fragment/wizardintro
的深层链接。这应该让主要 Activity 知道将用户发送到的特定后续片段。我用下面的代码表示所有可以接收此深层链接的片段。
companion object{
const val DEEP_LINK = "chatbot://fragment/wizardintro"
}
override fun onNewIntent(intent: Intent?) {
super.onNewIntent(intent)
intent?.data?.toString().also { deepLink ->
when (deepLink) {
IntroductionFragment.DEEP_LINK ->{
val categoryId = intent?.getLongExtra("categoryId", 0L)
val directions = MainNavGraphDirections.actionGlobalGoalWizard(categoryId)
navController.popBackStack()
navController.navigate(directions)
}
}
categoryId
时出现了我的问题在主 Activity 中并将其传递给下一个片段。我什么也没得到,只有默认的 Long 被传递。我认为 MainActivity 中的函数覆盖 fun onNewIntent(intent: Intent?) { } 接收任何意图。需要明确的是,这些意图是从解释器片段发送的,该片段在技术上是加载 json 的片段。在 json 中有一个 "route": {"type": "route", "url": "chatbot:///fragment/wizardintro"
intent?.data?.toString().also ...
将它们解包。然后在 when 语句中选择一个匹配
chatbot:///fragment/wizardintro
的片段
categoryId
相关的内容。
最佳答案
既然我们已经得出结论 MainActivity
没有得到 categoryId
, 你只需要传递 categoryId
与 deep-link
.
但是,不需要来自 Fragment
的任何通信。至Activity
.
您可以通过 Fragment
之间的通信达到相同的结果。至Fragment
, 和 Activity
至Fragment
.
您要做的是更仔细地查看 deep-links
和 android navigation
在 AndroidDocs 中,click here .
如您所知,有不同的方法可以解决这个问题,从 arguments
开始。对于每个片段。分配 categoryId
作为 argument
到 Fragment 将帮助您使用 navigationController
和 navigate
到新 Fragment
,同时通过 categoryId
给它。
现在,我知道您还希望使用 deep-link
启动它。 ; here 上也有很好的解释.根据docs ,您可以放置arguments
在 deep-links
以下列方式...
Placeholders in the form of
{placeholder_name}
match one or more characters. For example,http://www.example.com/users/{id}
matcheshttp://www.example.com/users/4
. The Navigation component attempts to parse the placeholder values into appropriate types by matching placeholder names to the defined arguments that are defined for the deep link destination. If no argument with the same name is defined, a default String type is used for the argument value.
navigation
是惊人的和有能力的东西,你只需要意识到它实际上可以做的一切。您甚至可以将其绑定(bind)到
BottomNavBar
, 代码量极少。
关于android-fragments - 通过 NavGraph 将数据从 Fragment 发送到 Main Activity,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59828945/
在我的 Activity 布局中,我有以下 NavHostFragment: 我的问题是如何以编程方式设置 navGraph?如何在 NavHostFragment 实例中转换 my_nav_hos
我有一个带有主要 Activity 的简单应用程序,一个 fragment frag_home ,以及一个名为 ItemFragment 的列表 fragment . 在 frag_home我有一个日
我开始学习共享 View 模型。 目前我在 Activity 中有 3 个 fragment ,其中 2 个在嵌套的 navGraph 中。 我想为它们创建共享的 navGraph viewModel
我有一个看起来像这样的 NavGraph: @Composable fun NavGraph ( navController: NavHostController ) { NavHos
我有 3 个项目的底部导航 View ,我的 navGraph看起来像这样: 带有嵌套 navGraph fragment 的底部导航 View 中的导航正常工作,但如果我
我正在尝试为我的订单管理应用程序构建以下导航: manage_orders/manage_orders/{locationId} manage_orders/manage_order_details/
我知道用 XML 构建 NavGraph 很容易,但是如何用 java 代码实现呢? 没有“app:navGraph”属性的 fragment XML 定义: TestActivity.java
我想在许多可组合项之间共享一个 View 模型。就像我们如何在 Activity 中的 fragment 之间共享 View 模型一样。 但是当我尝试这个 setContent { val n
我知道关于这个问题有几个问题和答案,但没有一个解决方案能解决我的问题。我得到一个 android.view.InflateException: Binary XML file line #11: Bi
在升级到最新的 fragment (1.2.2 版本)和导航(2.2.1 版本)后,为了防止出现 Lint 警告(将 fragment 替换为 FragmentContainerView),我将 fr
从 更新导航组合依赖后2.4.0-alpha03 至 2.4.0-alpha05 在尝试在可组合屏幕之间导航(例如从 taskComposable 导航到 listComposable 屏幕)后,我遇
在我的应用程序中,我使用具有多个片段架构的单个 Activity ,并使用导航库在它们之间导航。在其中一个片段中,我有多个类别,每个类别都与一个 ID 相关联。当点击一个类别时,我会使用下面的代码将用
Google 通过将 SharedViewModel 限定在 Activity 范围内声明在 Fragment 之间进行通信时使用 SharedViewModel。 在单个 Activity 应用程序
我正在使用 刀柄 对于 依赖注入(inject) 专门用于注入(inject)查看型号 . 还想范围 ViewModel 到 NavGraph .为此,请获取 ViewModelStoreOwner
我创建了一个 Android 应用程序,并从 Gallery 添加了“Navigation Drawer Activity”,我删除并重命名了菜单项。当我单击抽屉 Activity 中的任何菜单项启动
考虑这个例子。 对于身份验证,我们将使用 2 个屏幕 - 一个屏幕输入电话号码,另一个屏幕输入 OTP。 这两个屏幕都是在 Jetpack Compose 中制作的,对于 NavGraph,我们使用的
我试图在我的应用程序中实现导航功能。当我尝试构建应用程序时,输出显示: Android resource linking failed /home/mateusz/AndroidStudioProje
我是一名优秀的程序员,十分优秀!