- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我在Activity
中有以下代码
@Override
public void onPause() {
super.onPause();
if (isFinishing()) {
final LiveData<StickyNoteConfig> stickyNoteConfigLiveData = StickyNoteConfigRepository.INSTANCE.getStickyNoteConfig(mAppWidgetId);
stickyNoteConfigLiveData.removeObservers(this);
stickyNoteConfigLiveData.observe(this, stickyNoteConfig -> {
// Weird, I still can receive call back.
// I thought "this" is no longer active?
});
}
}
疑惑的是,虽然this
activity已经处于onPause
状态,但是Observer
还在被触发?根据https://developer.android.com/reference/android/arch/lifecycle/Lifecycle.State#STARTED
Started state for a LifecycleOwner. For an Activity, this state is reached in two cases:
after onStart call;
right before onPause call.
请问为什么会这样?
最佳答案
According to LiveData reference,
LiveData
is a data holder class that can be observed within a given lifecycle. This means that an Observer can be added in a pair with aLifecycleOwner
, and this observer will be notified about modifications of the wrapped data only if the pairedLifecycleOwner
is in active state.LifecycleOwner
is considered as active, if its state is STARTED or RESUMED.An observer added with a Lifecycle will be automatically removed if the corresponding Lifecycle moves to DESTROYED state.
现在,根据您的情况,LiveData
在 onPause()
observer(您的 Activity ) 中接收更新/strong> 方法,因为您的观察者尚未处于 DESTROYED 状态。
因此 LiveData 仍处于 Activity 状态以根据这些方法接收更新:
当活跃观察者的数量从 0 变为 1 时调用。此回调可用于了解正在使用此 LiveData,因此应保持最新。
&
当 Activity 观察者的数量从 1 变为 0 时调用。这并不意味着没有观察者,可能仍然存在观察者,但它们的生命周期状态不是 STARTED 或 RESUMED (就像 Activity返回堆栈)。
可以通过hasObservers()查看是否有观察者.
那么 观察者(您的 Activity ) 何时进入DESTROYED 状态?
对于 LifeCycleOwner
的默认实现,表示 Activity 在 onDestroy()
方法执行后以及在 onPause()
它遵循 LifeCycle 状态的相反顺序 RESUMED -> STARTED -> CREATED -> DESTROYED。
检查这个lifecycle graph .
希望对您有所帮助!
关于android - 为什么 LiveData 还要通知处于 onPause 状态的 Activity?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52617325/
我正在试用 Realm 以及包括 LiveData 在内的 Android 架构组件。 我一直在关注 Google 的应用程序架构指南: https://developer.android.com/t
我有两个 DAO、两个存储库和两个 POJO。有什么方法可以创建两个的一个 Livedata?我需要它来为 Recyclerview 制作单个列表。POJO 是类似的对象。 费用库: public c
使用PagedList,这里没有数据库备份,而是内存中的数据列表(称其为CachedDataList),可以通过fetchMore()填充功能。 有了 PositionalDataSource、Dat
我正在创建具有MVVM架构的应用程序,但遇到了一个问题:获取要在View中显示的LiveData列表。 在我的ViewModel中,我有一个getAll()函数,该函数使用Room从数据库中检索字符串
我有一个包含用户列表的 LiveData 对象,我正在尝试将数据传输到另一个 LiveData 对象以在其他地方使用。 我在 Room 中使用 MVVM,所以我从数据库和 ViewModel 中获取
我是测试新手,我想学习如何使用 MVVM 模式测试协程。我刚刚关注了 https://github.com/android/architecture-samples 项目并做了一些更改(删除了远程源)
我在存储库类中有一个方法如下,它从本地数据库或网络返回国家列表作为 LiveData: fun loadCountries(): LiveData>> { return object : Ne
official Android developer docs 中的协程 LiveData 示例使用 emit() 给出以下示例: val user: LiveData = liveData {
在文字游戏应用中,我在 Activity 和 fragment 之间共享一个模型: public class MainViewModel extends AndroidViewModel {
我正在尝试对一个 View 进行数据绑定(bind),该 View 应该显示通过 View 模型中的 LiveData 属性公开的数据,但我发现无法将 LiveData 内的对象绑定(bind)到 V
使用 lifecycle-viewmodel-ktx和 lifecycle-livedata-ktx并给出以下示例: ViewModel 实现: class AutocompletionViewMod
我正在尝试合并 Android Architecture GitHub example与数据绑定(bind)。为此,我想我必须在 UserViewModel 中添加一个从 LiveData> 到 Li
我需要将域对象映射到 UI 对象并使用实时分页列表显示。 我试图映射 LiveData>至 LiveData>和 map PositionalDataSource至 PositionalDataSou
假设我们有两个 LiveData 对象: LiveData> fooList; LiveData> barList; 并且可以通过某种方法(或构造函数)将 Foo 转换为 Bar 对象。将具有 Foo
我想使用 MutableLiveData 观察来自 ViewModel 的一些数据。问题是我使用子类和父类,并且与 LiveData 存在一些不兼容。我想在 Kotlin 中做的事情的一个例子: im
在 Java 中,我们访问 Livedata> 的第一个元素书籍使用 books[0] ,如何在 Kotlin 中做到这一点? 我已经尝试了一切。 //Java代码 books[0]; // Kotl
我阅读了许多关于 viewmodel 和 livedata 的教程,但我并没有在 mvvm 模式的 viewmodel 类中实际使用 livedata。提前致谢。 最佳答案 Android 开发人员通
我正在创建一个 TODO 列表应用程序,用户可以在其中添加要执行的任务。任务有几类。我有以下情况: viewModelMainActivity = new ViewModelProvi
我正在使用 Kotlin 和 Android 架构组件(LiveData 和 Room)制作一个 Android 应用。 我有一个显示用户的 Activity (存储在数据库中并使用带有 ViewMo
我在我的存储库中使用 AsyncTask,它用于设置存储库中的 LiveData。我如何使用转换从我的 ViewModel 观察这个 LiveData? 最佳答案 你可以忽略我的另一个答案。解决方案是
我是一名优秀的程序员,十分优秀!