Mamo1234
Mamo1234

Reputation: 998

android.app.Application cannot be cast to LifecycleOwner

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

Upvotes: 7

Views: 10997

Answers (4)

Supun Ayeshmantha
Supun Ayeshmantha

Reputation: 607

You can't cast an application as a lifecycleowner

try this within a coroutine scope

val uiScope= CoroutineScope(Dispatchers.Main)

uiScope.launch{
    result.observeForever {
   // it contains the livedata
}

}

and make sure to cancel the scope on viewmodel destory to avoid memory leaks

 override fun onCleared() {
        uiScope.cancel()
        super.onCleared()
    }

Upvotes: 0

MidasLefko
MidasLefko

Reputation: 4559

If you really do want to observe a LiveData for the Lifecycle of an Application then you can use ProcessLifecycleOwner

Upvotes: 2

Deˣ
Deˣ

Reputation: 4371

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'   

Upvotes: 3

CommonsWare
CommonsWare

Reputation: 1007321

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().

Upvotes: 22

Related Questions