简体   繁体   English

为什么我们使用 Lifecycle 感知组件 & Lifecycle Observer 在 Android 中的目的是什么?

[英]Why we use Lifecycle aware components & What is the purpose of Lifecycle Observer in Android?

We already have a Lifecycle in our Activity/Fragment then why will we use Lifecycle aware component & kindly guide me the main purpose of it.我们的 Activity/Fragment 中已经有一个生命周期,那么为什么我们要使用生命周期感知组件并请指导我它的主要目的。 And if we use lifecycle aware then why we use lifecycle that we knew already如果我们使用生命周期感知,那么为什么我们使用我们已经知道的生命周期

Yes the Android Activity has a so called lifecycle是的,Android Activity 有一个所谓的生命周期

But that has nothing to do with a 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.通过使用生命周期感知组件,您可以将依赖组件的代码从生命周期方法中移到组件本身中。

Here´sa tutorial hands-on to make it more easy to understand这是一个动手教程,使其更容易理解

So why did Google Android devs come up with this technique and others in the Android jetpack ?那么,为什么 Google Android 开发人员会在Android 喷气背包中提出这种技术和其他技术呢? Yes Lifecycle aware component discussed here comes from Android jetpack from 2018. .是的,这里讨论的生命周期感知组件来自 2018 年的 Android jetpack 。. They did it to make it easier for us to make robust Android apps.他们这样做是为了让我们更容易制作强大的 Android 应用程序。 They are doing it to get us closer to the SOLID principles.他们这样做是为了让我们更接近SOLID原则。 Before that, developers were coming up with their own styles using homemade MVC,MVVC or MVP.在此之前,开发人员正在使用自制的 MVC、MVVC 或 MVP 提出自己的风格。 Big discussions on Singletons technique for years and of course how we best do the separation of concern and Single responsibility principle多年来关于单身人士技术的大讨论,当然还有我们如何最好地做到关注点分离和单一责任原则

Now all of that is solved for us with Android jetpack现在这一切都用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在面向对象的计算机编程中,SOLID 是五个设计原则的助记首字母缩写词,旨在使软件设计更易于理解、灵活和可维护

WIKI维基

Lifecycle Observer helps you to listen app background and foreground events. Lifecycle Observer 可帮助您监听应用程序后台和前台事件。

Please find below link for implementation details :请在下面的链接中找到实施细节:

Lifecycler Observer Implementation生命周期观察者实现

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM