Unfortunately I can't use getApplication() as LifecycleOwner, because android.app.Application does not seem to implement it.
Why does android.app.Application not implement LifecycleOwner?
Code where I try to use getApplication() as LifecycleOwner for LiveData:
result.observe(getApplication(), ....);
Gradle:
apply plugin: 'com.android.application'
android {
compileSdkVersion 28
defaultConfig {
applicationId "XXXX"
minSdkVersion 15
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
vectorDrawables.useSupportLibrary = true
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility = '1.8'
targetCompatibility = '1.8'
}
}
dependencies {
// GENERAL
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.appcompat:appcompat:1.0.2'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation 'com.google.android.material:material:1.1.0-alpha07'
implementation "androidx.room:room-runtime:2.1.0"
annotationProcessor "androidx.room:room-compiler:2.1.0"
implementation "androidx.lifecycle:lifecycle-extensions:2.0.0"
annotationProcessor "androidx.lifecycle:lifecycle-compiler:2.0.0"
implementation "androidx.navigation:navigation-fragment:2.1.0-alpha05"
implementation "androidx.navigation:navigation-ui:2.1.0-alpha05"
// Third party libraries
// Jetbrains
implementation 'org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.3.31'
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.0.0'
// Glide
implementation 'com.github.bumptech.glide:glide:4.9.0'
annotationProcessor 'com.github.bumptech.glide:compiler:4.9.0'
// Fotoapparat
implementation 'io.fotoapparat:fotoapparat:2.7.0'
// TEST
testImplementation 'junit:junit:4.12'
// ANDROID TEST
androidTestImplementation 'androidx.test:runner:1.2.0'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
androidTestImplementation "androidx.room:room-testing:2.1.0"
}
Error:
java.lang.ClassCastException: ...
android.app.Application cannot be cast to androidx.lifecycle.LifecycleOwner
Why does android.app.Application not implement LifecycleOwner?
It does not have a lifecycle. It is created, then exists for the entire existence of the process.
Code where I try to use getApplication() as LifecycleOwner for LiveData:
Either use something with an actual lifecycle (activity, fragment, etc.) or use observeForever()
.
You cannot cast the Application
class to LifecycleOwner
.
Also I would ask you to upgrade your dependency. There were some issues related to LifecyclerOwner in earlier releases
Try updating your
implementation 'androidx.appcompat:appcompat:1.0.2'
to something like:
implementation 'androidx.appcompat:appcompat:1.1.0-beta01'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With