导航架构组件-活动 [英] Navigation Architecture Component - Activities

查看:44
本文介绍了导航架构组件-活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在关注导航架构组件中的文档>了解此新导航系统的工作原理.

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, a Fragment needs to be hosted containing the actual view?

活动会在不久的将来实现 NavHost 接口吗?

-已更新-

基于ianhanniballake的答案,我了解每个活动都包含其自己的导航图.但是,如果我想使用导航组件从一个活动转到另一个活动(替换旧的" startActivity 调用),则可以使用活动目标.什么不是活动目标,因为有关迁移的文档没有做任何详细介绍:

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 有什么好处吗?
  • 使用导航组件时从活动中走出来的正确方法是什么?
  • 推荐答案

    我设法通过使用 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)
    

    这篇关于导航架构组件-活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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