Android ライフサイクル フレームワークはメッセージ導入を使用します。
公式サイト紹介アドレス:
https://developer.android.com/jetpack/androidx/releases/lifecycle
アドレスの主な説明は次のとおりです。
1. コンポーネントのバージョン情報の説明を変更します。
2. リリース バージョンではこれらの点が変更されます。バグやその他の関連コンテンツが含まれます。
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 をサポートします。。。ちょっとした穴。
ですので、取り外しても大丈夫です。
エラーメッセージは次のとおりです。
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 です。コンテンツ プロバイダーです。
アプリ起動時に初期化されます。ContentProvider の onCreate() ライフサイクル メソッドが呼び出されます。
8. 以下では、ライフサイクル コードを詳細に分析します。