当前位置:网站首页>2022. Let me take you from getting started to mastering jetpack architecture components - lifecycle
2022. Let me take you from getting started to mastering jetpack architecture components - lifecycle
2022-07-01 13:47:00 【InfoQ】
Preface
Introduce


Lifecycle
I Believe , When you first saw Lifecycle when , You will have the following four questions :
- Lifecycle What is it ?
- What is it used for ?
- What are its advantages ?
- How to use it ?
Lifecycle What is it? :
- life: life ,( Something ) The lifetime of
- cycle: cycle
Lifecycle What to do :
lifecycle What are the advantages ?
- Lifecycler It realizes the separation of execution logic and activities , Code decouples and increases the readability of the code
- Lifecycler Remove the listener at the end of the activity , Avoid the problem of declaration cycle
How to use Lifecycle Well ?
- Lifecycle
- Lifecycle Is an abstract class , The implementation subclass is LifecycleRegistry
class LifecycleRegistry extends Lifecycle{
.......
}
- lifecycleRegister
- lifecycle The only subclass of , Subscription callback logic used to trigger its own state and related observers when the life cycle changes
- LifecycleOwner
- Used to connect objects with a lifecycle
public interface LifecycleOwner {
@NonNull
Lifecycle getLifecycle();
}
- LifecycleObserver
- Lifecycle The observer
- State(Lifecycle Inside the abstract class of )
- Indicates the current lifecycle state
public enum State {
DESTROYED,
INITIALIZED,
CREATED,
STARTED,
RESUMED;
public boolean isAtLeast(@NonNull State state) {
return compareTo(state) >= 0;
}
}
- Event(Lifecycle Inside the abstract class of )
- The current life cycle changes the corresponding events
public enum Event {
ON_CREATE,
ON_START,
ON_RESUME,
ON_PAUSE,
ON_STOP,
ON_DESTROY,
ON_ANY;
......
}
Lifecycle Use :
- gradle References to
dependencies {
def lifecycle_version = "2.5.0-rc01"
def arch_version = "2.1.0"
kapt "androidx.lifecycle:lifecycle-compiler:$lifecycle_version"
implementation "androidx.lifecycle:lifecycle-common-java8:$lifecycle_version"
implementation "androidx.lifecycle:lifecycle-service:$lifecycle_version"
implementation "androidx.lifecycle:lifecycle-process:$lifecycle_version"
implementation "androidx.lifecycle:lifecycle-reactivestreams-ktx:$lifecycle_version"
testImplementation "androidx.arch.core:core-testing:$arch_version"
}
> Here we can find that we have imported lifecycle-service,lifecycle-process These two components , because , In the new edition of SDK in ,Activity/Fragment It has been implemented by default LifecycleOwner Interface , in the light of Service,Android Provided separately LifeCycleService, Not like it Activity、Fragment By default LifeCycleOwner. in the light of Application,Android Provides ProcessLifeCycleOwner For listening to the entire application lifecycle .
LifecycleObserver
import android.util.Log
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleObserver
import androidx.lifecycle.OnLifecycleEvent
//OnLifecycleEvent It's out of date
class MyLifecycleTest : LifecycleObserver {
companion object{
private const val TAG = "MyLifecycleTest"
}
@OnLifecycleEvent(Lifecycle.Event.ON_CREATE)
fun create() {
Log.d(TAG, "create: ")
}
@OnLifecycleEvent(Lifecycle.Event.ON_START)
fun start() {
Log.d(TAG, "start: ")
}
@OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
fun resume() {
Log.d(TAG, "resume: ")
}
@OnLifecycleEvent(Lifecycle.Event.ON_PAUSE)
fun pause() {
Log.d(TAG, "pause: ")
}
@OnLifecycleEvent(Lifecycle.Event.ON_STOP)
fun stop() {
Log.d(TAG, "stop: ")
}
@OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
fun destroy() {
Log.d(TAG, "destroy: ")
}
@OnLifecycleEvent(Lifecycle.Event.ON_ANY)
fun any() {
// Log.d(TAG, "any: ")
}
}
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
lifecycle.addObserver(MyLifecycleTest())
}
}

- Use
DefaultLifecycleObserver
DefaultLifecycleObserver@OnLifecycleEventMyDefaultLifecycleObserver Inherited from DefaultLifecycleObserverimport android.util.Log
import androidx.lifecycle.DefaultLifecycleObserver
import androidx.lifecycle.LifecycleOwner
class MyDefaultLifecycleObserver : DefaultLifecycleObserver {
companion object {
private const val TAG = "MyDefaultLifecycleObserver"
}
override fun onCreate(owner: LifecycleOwner) {
super.onCreate(owner)
Log.d(TAG, "onCreate: ")
}
override fun onStart(owner: LifecycleOwner) {
super.onStart(owner)
Log.d(TAG, "onStart: ")
}
override fun onResume(owner: LifecycleOwner) {
super.onResume(owner)
Log.d(TAG, "onResume: ")
}
override fun onPause(owner: LifecycleOwner) {
super.onPause(owner)
Log.d(TAG, "onPause: ")
}
override fun onStop(owner: LifecycleOwner) {
super.onStop(owner)
Log.d(TAG, "onStop: ")
}
override fun onDestroy(owner: LifecycleOwner) {
super.onDestroy(owner)
Log.d(TAG, "onDestroy: ")
}
}
MyApplicationaddObserver()ObserverLifecycleRegistryimport android.app.Application
import androidx.lifecycle.ProcessLifecycleOwner
class MyApplication : Application() {
override fun onCreate() {
super.onCreate()
ProcessLifecycleOwner.get().lifecycle.addObserver(MyDefaultLifecycleObserver())
}
}


Several examples Lifecycle Usage scenarios of :
- Retrofit coordination Lifecycle management Http Life cycle
- Reading is recommended here :https://mp.weixin.qq.com/s/omCnmMX3XVq-vnKoDnQXTg
- Pause and resume animation
- Pause and play of video
- Handler Remove messages from
- ........
Here are a few questions :
- Lifecycle There are several ways to create ( What's the difference , Which one is recommended )?
- Lifecycle How to synchronize the lifecycle ?
- Event Events and State What is the relationship between the State ?
- Lifecycle Registration of , distributed , What is the process of perception ?
- What is a nested event ? When did it happen ?Lifecycle How to solve it ?
边栏推荐
猜你喜欢

A new book by teacher Zhang Yujin of Tsinghua University: 2D vision system and image technology (five copies will be sent at the end of the article)

Flow management technology

Beidou communication module Beidou GPS module Beidou communication terminal DTU

Spark source code (V) how does dagscheduler taskscheduler cooperate with submitting tasks, and what is the corresponding relationship between application, job, stage, taskset, and task?

Chen Yu (Aqua) - Safety - & gt; Cloud Security - & gt; Multicloud security

5. Use of ly tab plug-in of header component

2022年PMP项目管理考试敏捷知识点(6)

玩转gRPC—不同编程语言间通信

Qtdeisgner, pyuic detailed use tutorial interface and function logic separation (nanny teaching)

龙蜥社区开源 coolbpf,BPF 程序开发效率提升百倍
随机推荐
算网融合赋能行业转型,移动云点亮数智未来新路标
ArrayList扩容机制以及线程安全性
Kongsong (Xintong Institute) - cloud security capacity building and trend in the digital era
受益互联网出海 汇量科技业绩重回高增长
2022上半年英特尔有哪些“硬核创新”?看这张图就知道了!
单工,半双工,全双工区别以及TDD和FDD区别
新手准备多少钱可以玩期货?农产品可以吗?
Solution to 0xc000007b error when running the game [easy to understand]
2.15 summary
自定义注解实现验证信息的功能
1.8 new features list
QT community management system
使用net core 6 c# 的 NPOI 包,读取excel..xlsx单元格内的图片,并存储到指定服务器
关于佛萨奇2.0“Meta Force原力元宇宙系统开发逻辑方案(详情)
Understand the window query function of tdengine in one article
机器学习总结(一):线性回归、岭回归、Lasso回归
Terminal identification technology and management technology
【剑指Offer】54. 二叉搜索树的第k大节点
Applet - multiple text line breaks in view
“国防七子”经费暴增,清华足足362亿元,甩第二名101亿 |全国高校2022预算大公开...