当前位置:网站首页>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 ?
边栏推荐
- QT learning management system
- C语言基础知识
- [241. Design priority for operation expression]
- Error:Kotlin: Module was compiled with an incompatible version of Kotlin. The binary version of its
- Analysis report on the development trend and Prospect of new ceramic materials in the world and China Ⓐ 2022 ~ 2027
- leetcode 322. Coin Change 零钱兑换(中等)
- Detailed explanation of leetcode reconstruction binary tree [easy to understand]
- Flow management technology
- A Fletter version of Notepad
- MySQL六十六问,两万字+五十图详解!复习必备
猜你喜欢

Self cultivation of open source programmers who contributed tens of millions of lines of code to shardingsphere and later became CEO

leetcode622.设计循环队列(C语言)

进入前六!博云在中国云管理软件市场销量排行持续上升

9. Use of better scroll and ref

使用net core 6 c# 的 NPOI 包,读取excel..xlsx单元格内的图片,并存储到指定服务器

Beidou communication module Beidou GPS module Beidou communication terminal DTU

佩服,阿里女程序卧底 500 多个黑产群……

Animesr: learnable degradation operator and new real world animation VSR dataset

详细讲解面试的 IO多路复用,select,poll,epoll

玩转MongoDB—搭建MongoDB集群
随机推荐
Introduction to topological sorting
Station B was scolded on the hot search..
Yan Rong looks at how to formulate a multi cloud strategy in the era of hybrid cloud
Analysis report on the development trend and Prospect of new ceramic materials in the world and China Ⓐ 2022 ~ 2027
Blind box NFT digital collection platform system development (build source code)
1.8新特性-List
Camp division of common PLC programming software
Uni app realizes advertisement scroll bar
leetcode 322. Coin Change 零钱兑换(中等)
When you really learn databinding, you will find "this thing is really fragrant"!
玩转MongoDB—搭建MongoDB集群
佩服,阿里女程序卧底 500 多个黑产群……
运行游戏时出现0xc000007b错误的解决方法[通俗易懂]
word2vec训练中文词向量
9. Use of better scroll and ref
Investment analysis and prospect prediction report of global and Chinese p-nitrotoluene industry Ⓙ 2022 ~ 2027
B站被骂上了热搜。。
String input function
Liu Dui (fire line safety) - risk discovery in cloudy environment
Global and Chinese silicone defoamer production and marketing demand and investment forecast analysis report Ⓨ 2022 ~ 2027