Android Lifecycle

Android Lifecycle framework uses message introduction:

Official website introduction address:

https://developer.android.com/jetpack/androidx/releases/lifecycle

The main description of the address is as follows: 

1. Change the component version information description.

2. The release version modifies those things. Including bugs and other related content.

3. To use gradle, you need to import those related packages.

The following is the stable version as follows

    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. The minimum supported sdk version of this version is 24. . . A little pit.

 //implementation "androidx.lifecycle:lifecycle-common-java8:$lifecycle_version" This version supports at least sdk version 24. . . A little pit.

So it's OK to remove it.

The error message is as follows:

Default interface methods are only supported starting with Android N (--min-api 24): 
void androidx.lifecycle.DefaultLifecycleObserver.onCreate(androidx.lifecycle.LifecycleOwner)

5. Change to 24. After compiling, decompile and get the following information to get the AndroidManifest.xml file as follows:

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. Which we can see. annotationProcessor "androidx.lifecycle:lifecycle-compiler:$lifecycle_version" tool will

Help us put the following code into 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 is a ContentProvider. Content provider.

When the App starts, it will be initialized. The onCreate() lifecycle method of the ContentProvider will be called.

8. Below we will analyze the Lifecycle code in detail

Guess you like

Origin blog.csdn.net/shaohuazuo/article/details/104007973