Android(kotlin)JetPack系列学习——1. Lifecycle
创始人
2024-04-02 07:06:35
0

文章目录

    • 1. LifeCycle(生命周期管理)简介
    • 2. 使用
    • 3. 效果如下

1. LifeCycle(生命周期管理)简介

lifeCycle主要是用来解耦的,LifeCycle可以帮助开发者创建可感知生命周期的组件。这样,组件便能够在其内部管理自己的生命周期,从而降低模块间的耦合度,并降低内存泄漏发生的可能性。LifeCycle不只对Activity/Fragment有用,在Service和Application中也能大显身手。其作用如下:

  • 使用Lifecycle解耦页面与组件
  • 使用LifecycleService解耦Service与组件
  • 使用ProcessLifecycleOwner监听应用程序周期

如果没有Lifecycle,则需要在onResume(), onStop()等生命周期函数中去写,项目一旦大了,就难以维护。

2. 使用

Step1: 添加依赖

plugins {id 'com.android.application'id 'org.jetbrains.kotlin.android'id 'kotlin-kapt'
}android {compileSdk 32defaultConfig {applicationId "com.jetpackstudy"minSdk 21targetSdk 32versionCode 1versionName "1.0"testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"}buildTypes {release {minifyEnabled falseproguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'}}compileOptions {sourceCompatibility JavaVersion.VERSION_1_8targetCompatibility JavaVersion.VERSION_1_8}kotlinOptions {jvmTarget = '1.8'}
}dependencies {implementation 'androidx.core:core-ktx:1.7.0'implementation 'androidx.appcompat:appcompat:1.3.0'implementation 'com.google.android.material:material:1.4.0'implementation 'androidx.constraintlayout:constraintlayout:2.0.4'testImplementation 'junit:junit:4.13.2'androidTestImplementation 'androidx.test.ext:junit:1.1.3'androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'// lifecycle监听整个应用程序的生命周期implementation 'androidx.lifecycle:lifecycle-process:2.5.1'implementation 'androidx.lifecycle:lifecycle-livedata-ktx:2.3.1'implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.3.1'// viewModel的获取implementation 'androidx.fragment:fragment-ktx:1.5.2'// roomimplementation "androidx.room:room-runtime:2.4.3"kapt "androidx.room:room-compiler:2.4.3"// navigationimplementation "androidx.navigation:navigation-fragment-ktx:2.5.2"implementation "androidx.navigation:navigation-ui-ktx:2.5.2"// pagingimplementation "androidx.paging:paging-runtime:3.1.1"
}

添加里面对应的依赖即可

Step2: 实现DefaultLifecycleObserver接口

package com.jetpackstudy.lifecycleimport android.util.Log
import androidx.lifecycle.DefaultLifecycleObserver
import androidx.lifecycle.LifecycleOwner/***  author  : ctb*  date    : 2022/10/29 22:18*  desc    : 监听生命周期*/private const val TAG = "MyDefaultLifecycleObser"
class MyDefaultLifecycleObserver: DefaultLifecycleObserver{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: ")}}

Step3: 在Activity中添加观察者:

package com.jetpackstudyimport androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import com.jetpackstudy.lifecycle.MyDefaultLifecycleObserverclass MainActivity : AppCompatActivity() {override fun onCreate(savedInstanceState: Bundle?) {super.onCreate(savedInstanceState)setContentView(R.layout.activity_main)lifecycle.addObserver(MyDefaultLifecycleObserver())		// 仅需添加一行代码即可完成}
}

3. 效果如下

运行后的效果如下:
在这里插入图片描述

相关内容

热门资讯

监控摄像头接入GB28181平... 流程简介将监控摄像头的视频在网站和APP中直播,要解决的几个问题是:1&...
Windows10添加群晖磁盘... 在使用群晖NAS时,我们需要通过本地映射的方式把NAS映射成本地的一块磁盘使用。 通过...
protocol buffer... 目录 目录 什么是protocol buffer 1.protobuf 1.1安装  1.2使用...
在Word、WPS中插入AxM... 引言 我最近需要写一些文章,在排版时发现AxMath插入的公式竟然会导致行间距异常&#...
Fluent中创建监测点 1 概述某些仿真问题,需要创建监测点,用于获取空间定点的数据࿰...
educoder数据结构与算法...                                                   ...
MySQL下载和安装(Wind... 前言:刚换了一台电脑,里面所有东西都需要重新配置,习惯了所...
MFC文件操作  MFC提供了一个文件操作的基类CFile,这个类提供了一个没有缓存的二进制格式的磁盘...
有效的括号 一、题目 给定一个只包括 '(',')','{','}'...
【PdgCntEditor】解... 一、问题背景 大部分的图书对应的PDF,目录中的页码并非PDF中直接索引的页码...