Android Lifecycle 框架使用消息介绍:
官网介绍地址:
https://developer.android.com/jetpack/androidx/releases/lifecycle
该地址主要说明如下:
1.改组件版本信息说明。
2.发布版本修改那些东西。包括bug等相关内容。
3.使用gradle需要导入那些相关的包。
下面是稳定版如下
def lifecycle_version = "2.1.0"
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
// ViewModel and LiveData
implementation "androidx.lifecycle:lifecycle-extensions:$lifecycle_version"
// alternatively - just ViewModel
implementation "androidx.lifecycle:lifecycle-viewmodel:$lifecycle_version"
//For Kotlin use lifecycle-viewmodel-ktx
// alternatively - just LiveData
implementation "androidx.lifecycle:lifecycle-livedata:$lifecycle_version"
//alternatively - Lifecycles only (no ViewModel or LiveData). Some UI
//AndroidX libraries use this lightweight import for Lifecycle
implementation "androidx.lifecycle:lifecycle-runtime:$lifecycle_version"
// For Kotlin use kapt instead of annotationProcessor
annotationProcessor "androidx.lifecycle:lifecycle-compiler:$lifecycle_version"
// alternately - if using Java8, use the following instead of lifecycle-compiler
implementation "androidx.lifecycle:lifecycle-common-java8:$lifecycle_version"
// optional - ReactiveStreams support for LiveData
implementation "androidx.lifecycle:lifecycle-reactivestreams:$lifecycle_version" // For Kotlin use lifecycle-reactivestreams-ktx
// optional - Test helpers for LiveData
testImplementation "androidx.arch.core:core-testing:$lifecycle_version"
4.该版本最低支持sdk版本是24。。。小小有点坑。
//implementation "androidx.lifecycle:lifecycle-common-java8:$lifecycle_version" 该版本最低支持sdk版本是24。。。小小有点坑。
所以去掉就OK了。
错误信息如下:
Default interface methods are only supported starting with Android N (--min-api 24):
void androidx.lifecycle.DefaultLifecycleObserver.onCreate(androidx.lifecycle.LifecycleOwner)
5.修改成24。编译之后反编译有如下信息得到AndroidManifest.xml 文件如下:
AndroidManifest.xml+ buffers
1 <?xml version="1.0" encoding="utf-8" standalone="no"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" android:compile
2 <application android:allowBackup="true" android:appComponentFactory="androidx.core.app.CoreComponentFactory" android:debuggable="true"
3 <activity android:name="com.octopus.myapplication.MainActivity">
4 <intent-filter>
5 <action android:name="android.intent.action.MAIN"/>
6 <category android:name="android.intent.category.LAUNCHER"/>
7 </intent-filter>
8 </activity>
9 <provider android:authorities="com.octopus.myapplication.lifecycle-process"
10 android:exported="false" android:multiprocess="true"
11 android:name="androidx.lifecycle.ProcessLifecycleOwnerInitializer"/>
12 </application>
13 </manifest>
~
~
6. 其中我们可以看到。 annotationProcessor "androidx.lifecycle:lifecycle-compiler:$lifecycle_version" 工具会
帮我们如下代码到Android AndroidManifest.xml中。
<provider android:authorities="com.octopus.myapplication.lifecycle-process"
android:exported="false" android:multiprocess="true"
android:name="androidx.lifecycle.ProcessLifecycleOwnerInitializer"/>
7. androidx.lifecycle.ProcessLifecycleOwnerInitializer 是一个ContentProvider. 内容提供者。
在App启动的的时候,将会被初始化。 ContentProvider的onCreate()生命周期方法将会被调用。
8.下面我们将对Lifecycle代码进行详细分析