gpt4 book ai didi

android - 将导航组件与多个 Activity 一起使用

转载 作者:行者123 更新时间:2023-12-02 11:22:37 25 4
gpt4 key购买 nike

在 Android 文档中,它指出:

The Navigation component is designed for apps that have one main activity with multiple fragment destinations. The main activity is associated with a navigation graph and contains a NavHostFragment that is responsible for swapping destinations as needed. In an app with multiple activity destinations, each activity has its own navigation graph.

这是否意味着您无法使用导航组件从一个 Activity 导航到另一个 Activity ?看来确实如此。

第二个问题:如果我创建一个使用抽屉式导航的应用程序,则在添加具有抽屉式导航的 Activity 时创建的默认代码已经包含用于管理从一个抽屉项目到另一个抽屉项目的导航的代码。那么导航组件在这里也没什么用吗?

Google 是否希望我们只创建单一 Activity 应用?

最佳答案

Does Google want us to be creating only single activity apps?

单一 Activity 架构是您可以迈向的方向。它不受 Google 限制(只是推荐)。该架构有其自身的优点和缺点。您不必仅仅为了添加导航组件而拆掉整个应用程序。评估并决定是否值得承受痛苦。

Does this mean that you cannot use the Navigation component to navigate from one activity to another

不,您可以使用导航组件来替换 startActivity 调用。只需将第二个 Activity 导航图添加到第一个 Activity 导航图,然后使用导航 Controller 在两者之​​间导航即可。

findNavController().navigate(directions)

这是它的迁移指南 https://developer.android.com/guide/navigation/navigation-migrate#add

如果您想要使用不同的 Activity ,您可以评估是否需要不同的 Activity 或不同的任务。

If I create an app that uses the navigation drawer, the default code that created when you add an activity that is to have a navigation drawer already has code for managing navigation from one drawer item to another. So is the Navigation component also kind of useless here?

instead of using the default code for a navigation drawer to build your own navigation drawer that is more inline with the Navigation component

问题是您不必构建自定义组件或任何复杂的东西。实际上,使用导航组件(在 NavigationUI 类的帮助下)简化了抽屉布局及其监听器的代码。

this link ,该文档帮助您在使用抽屉导航和底部导航 View 时实现导航组件。

关于生成的模板,这些模板已经过时,需要升级。

引用文献:

https://developer.android.com/guide/navigation/navigation-migrate https://developer.android.com/guide/navigation/navigation-ui

关于android - 将导航组件与多个 Activity 一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56184301/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com