不幸的是,我不能将getApplication()用作LifecycleOwner,因为android.app.Application似乎没有实现它。
为什么android.app.Application不实现LifecycleOwner?
我尝试将getApplication()用作LiveData的LifecycleOwner的代码:
AC_ARG_WITH([readline],
AS_HELP_STRING([--without-readline], [disable support for readline]),
[],
[with_readline=yes]
)
AS_IF([test x$with_readline = xyes],
[
AC_DEFINE([WITH_READLINE], [1],
[Define to 1 if readline support is enabled.])
CFLAGS="-I${withval} ${CFLAGS}"
LDFLAGS="-L${withval} ${LDFLAGS}"
]
)
等级:
result.observe(getApplication(), ....);
错误:
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"
}
答案 0 :(得分:2)
为什么android.app.Application不实现LifecycleOwner?
它没有生命周期。它被创建,然后存在于整个过程中。
我尝试将getApplication()用作LiveData的LifecycleOwner的代码:
要么使用具有实际生命周期的内容(活动,片段等),要么使用observeForever()
。
答案 1 :(得分:2)
您无法将the Application
类投射到LifecycleOwner
。
我也请您升级您的依赖关系。在早期版本中,有一些与LifecyclerOwner相关的问题
尝试更新您的
implementation 'androidx.appcompat:appcompat:1.0.2'
类似于:
implementation 'androidx.appcompat:appcompat:1.1.0-beta01'
答案 2 :(得分:0)
如果您确实想观察 LiveData
的生命周期的 Application
,那么您可以使用 ProcessLifecycleOwner