Updating Data in Android Slices using onSlicePinned

Viewed 301

Since lengthly operations can't be done inside onBindSlice(), I am fetching data from server from onSlicePinned() method and then showing it in slices. But I can't understand when is this function called.

I thought it would be called every time we fresh start Assistant (i.e. starting Assistant after killing it). It is working while testing on Slice-Viewer app, every time i kill slice viewer and start again then onSlicePinned() is called along with onSliceBind(), but not in case of Google Assistant. So is this a bug or should be the way it is? And if this is how onSlicePinned() should work, then is there any way i can fetch data every time onBindSlice() is called after starting app.

1 Answers

To load content from a server into a Slice you should follow the delayed content best practices here:

https://developer.android.com/guide/slices/templates#delayed_content

The tl;dr is, you should return something immediately, then go off and load/process your content, once the content is ready, call notifyChange() which will result in onBindSlice() being called again where you can return the Slice with the new content.

Related