导航架构组件 - 活动
我一直在关注导航架构组件的文档> 了解这个新导航系统的工作原理.
I've been following the docs from Navigation Architecture Component to understand how this new navigation system works.
要从一个屏幕返回/返回到另一个屏幕,您需要一个实现 NavHost
接口的组件.
To go/back from one screen to another you need a component which implements NavHost
interface.
NavHost 是一个空视图,因此目的地被交换并当用户浏览您的应用时退出.
The NavHost is an empty view whereupon destinations are swapped in and out as a user navigates through your app.
但是,目前似乎只有 Fragments 实现了 NavHost
But, it seems that currently only Fragments implement NavHost
导航架构组件的默认 NavHost 实现是 NavHostFragment.
The Navigation Architecture Component’s default NavHost implementation is NavHostFragment.
所以,我的问题是:
即使我有一个可以用
Activity
实现的非常简单的屏幕,为了使用这个新的导航系统,Fragment
需要托管包含实际视图?
Even if I have a very simple screen which can be implemented with an
Activity
, in order to work with this new navigation system, aFragment
needs to be hosted containing the actual view?
Activity
会在不久的将来实现 NavHost
接口吗?
Will Activity
implement NavHost
interface in a near future?
--更新--
根据 ianhanniballake 的回答,我了解到每个活动都包含自己的导航图.但是,如果我想使用导航组件从一个活动转到另一个活动(替换旧"startActivity
调用),我可以使用 activity destination
.什么是活动目的地
我不清楚,因为迁移文档不涉及任何细节:
Based on ianhanniballake's answer, I understand that every activity contains its own navigation graph. But if I want to go from one activity to another using the nav component (replacing "old" startActivity
call), I can use activity destinations
. What is activity destinations
is not clear to me because the docs for migration don't go into any detail:
然后可以通过将活动目的地添加到导航图中来链接单独的活动,替换整个代码库中 startActivity() 的现有用法.
Separate Activities can then be linked by adding activity destinations to the navigation graph, replacing existing usages of startActivity() throughout the code base.
- 使用
ActivityNavigator
代替startActivity
有什么好处吗? - 使用导航组件时,从活动开始的正确方法是什么?
- Is there any benefit on using
ActivityNavigator
instead ofstartActivity
? - What is the proper way to go from activities when using the nav component?
我使用 ActivityNavigator
成功地从一个活动导航到另一个活动,而无需托管 Fragment.
I managed to navigate from one activity to another without hosting a Fragment by using ActivityNavigator
.
ActivityNavigator(this)
.createDestination()
.setIntent(Intent(this, SecondActivity::class.java))
.navigate(null, null)