android/codelab-slices-basic-codelab

Bug: notifyChange doesnt' work on SliceProvider

XinyueZ opened this issue · 2 comments

In updateTemperature the call context.getContentResolver().notifyChange(uri, null); doesn't fire chain on onBindSlice in MySliceProvider.

I see TODO on parent class, however, is there any workarounds?

I fought with this for a day and here is what worked for me.
Simply create a function like fetchData(sliceUri) which calls pendingIntent.send() and then in onBindSlice(), add something like this.
In my code, scheduleContainer is the static object (there has to be a better way and I am sure they are working on it).

The reason the notifyChange() callback gets missed is because system has not had chance to fully complete slice process (my guess). I tried to call from onSlicePinned() callback, but it never gets called and I could not find another callback to start the pending intent.

        val isLoading = scheduleContainer == null
        if (isLoading) {
            // calling fetchData() here does not give system chance to complete slice creation and
            // thus callback from BroadcastReceiver/ContentProvider is missed.
            handler.postDelayed({ fetchData(sliceUri) }, 500)
        }

I rewrote the codelab from scratch Kotlin. :) I am closing this but if you see it in the new codelab, please reopen and I will look at it ASAP. Thanks!