我们的 Activity/Fragment 中已经有一个生命周期,那么为什么我们要使用生命周期感知组件并请指导我它的主要目的。 如果我们使用生命周期感知,那么为什么我们使用我们已经知道的生命周期
最佳答案
是的,Android Activity 有一个所谓的 lifecycle
但这与 Lifecycle aware component 无关
Lifecycle-aware components perform actions in response to a change in the lifecycle status of another component, such as activities and fragments. These components help you produce better-organized, and often lighter-weight code, that is easier to maintain.
A common pattern is to implement the actions of the dependent components in the lifecycle methods of activities and fragments. However, this pattern leads to a poor organization of the code and to the proliferation of errors. By using lifecycle-aware components, you can move the code of dependent components out of the lifecycle methods and into the components themselves.
这是一个tutorial动手操作,让其更容易理解
那么,为什么 Google Android 开发人员在 Android jetpack 中提出了这项技术和其他技术? ?是的,这里讨论的生命周期感知组件来自Android jetpack from 2018. 。他们这样做是为了让我们更轻松地开发强大的 Android 应用程序。他们这样做是为了让我们更接近 SOLID原则。在此之前,开发人员使用自制的 MVC、MVVC 或 MVP 提出自己的风格。多年来关于单例技术的大讨论,当然还有我们如何最好地做到 separation of concern and Single responsibility principle
现在所有这些都已为我们解决了 Android jetpack
In object-oriented computer programming, SOLID is a mnemonic acronym for five design principles intended to make software designs more understandable, flexible and maintainable
关于java - 为什么我们使用 Lifecycle 感知组件以及 Android 中 Lifecycle Observer 的用途是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56363554/