问题
I have two activities who read the same database table. In activity A
I have an AsyncTaskLoader
and LoaderCallbacks
objects with the following onLoadFinished
code (in Scala):
def onLoadFinished(loader: CursorLoader, cursor: Cursor) = {
android.util.Log.d(TAG, "got results")
cursor.setNotificationUri(ct.getContentResolver, observePath)
cursor registerContentObserver new ContentObserver(new Handler) {
override def onChange(selfChange: Boolean) = {
android.util.Log.d(TAG, "change registered")
if (null != loader) loader.onContentChanged
}
override def deliverSelfNotifications = true
}
}
Now, if I go to activity B
, update database there and call this:
getContentResolver.notifyChange(observePath, null)
then onLoadFinished
in activity A is never called although I do get "change registered"
output in console.
But if I set a few seconds delay so that I have time to return to activity A
before the code above is executed, then onLoadFinished
is called normally.
It seems LoaderCallbacks
methods are somehow dependent on the host activity being active but I don't want that, I want onLoadFinished
to be called even if activity A is in background. How can I achieve that?
来源:https://stackoverflow.com/questions/35609166/loadercallbacks-onloadfinished-not-called-if-activity-is-not-active