我目前正在学习新的Android Navigation Architecture Component (https://developer.android.com/topic/libraries/architecture/navigation/).

我对它的动机和概念有点困惑,以下是我的不确定因素:

  1. Android Navigation Architecture Component是为了消除在一个应用程序中使用多个活动的需要而设计的吗?也就是说,整个应用程序只需要一个活动,而所有其他页面都将是碎片?
  2. Does using Multiple Activities in the apps, but in the same time using the Android Navigation Architecture Component to navigate the Fragment actually violate the purpose of Android Navigation Architecture Component?

Example Scenario for Question 2:

enter image description here

推荐答案

In theory, the Navigation library supports any architecture you might want to use. Out of the box it can handle Activities and Fragments as navigation destinations, but you can plug in your own solution by implementing your own Navigator (as an example, see this article).

However, quoted / paraphrased from the Google I/O talk on Navigation:

What is my Activity actually meant to do?

Right now, some apps are very Activity-heavy, some are Fragment-heavy, or completely in a different system. We're moving towards a model where the Activity is more just an entry point into your app, rather than it being the owner of the content of your app. It's actually just going to store global state, for example global navigation like a navigation drawer or the bottom bar.

所以谷歌确实建议你的应用程序只有几个活动,因为你只需要它们作为切入点.例如,您可以有一个从启动器打开的,另一个是通过深度链接打开的.在此之后,当您的应用程序启动时,您可以使用片段在其中执行其他所有操作.

To summarize and directly answer your two questions:

  1. 导航体系 struct 组件本身并不"旨在消除使用多个活动的需要",但这是Google建议您在使用它时所做的事情.

  2. You can absolutely still use multiple Activities and multiple Fragments mixed together. You can even use a single Activity with purely View based navigation if you like. It's all up to you. If you find the Navigation library useful in combination with how you architect your app, use it.

    对于自定义目的地,库的工具可能不是很好(例如,可视化编辑器目前可能只支持活动和片段),但是您可以在代码中随心所欲地使用它.

Kotlin相关问答推荐

带有Spring Boot和Kotline的可嵌入实体

何时使用figureEach

Kotlin-elvis算子don';不使用map.get()

Kotlin Poet 的导入不明确

用于将 0.5 变为 0 的 round() 函数的模拟

如何在 Spring Boot 3 中为内部类提供运行时提示

如果带注释的成员未被特定块包围,则发出 IDE 警告

Kotlin - 协程未按预期执行

如何使用成员引用在 Kotlin 中创建属性的分层路径

Kotlin 日期格式从一种更改为另一种

Kotlin Compose,在行中对齐元素

如何为kotlin异常生成SerialVersionId?

Kotlin lambda 语法混淆

不推荐使用仅限生命周期的LifecycleEvent

Kotlin 对象 vs 伴生对象(companion-object) vs 包作用域(package scoped)方法

Kotlin 错误:public function exposes its 'public/*package*/' return type argument

var str:String是可变的还是不可变的?

Kotlin中对象和数据类的区别是什么?

Android room DAO 接口不适用于继承

如何将 Kotlin 的 `with` 表达式用于可空类型