2

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.

AdamK
  • 21,199
  • 5
  • 42
  • 58
Ashish Rana
  • 112
  • 5

1 Answers1

0

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.

AdamK
  • 21,199
  • 5
  • 42
  • 58
  • So, when is the onSlicePinned() and onSliceUnpinned() methods called. Does slices get unpinned when we close Google Assistant? – Ashish Rana Jun 27 '19 at 02:04
  • They can optionally be called by the app displaying the Slices if the app wants to pin the Slice in view and have it provide more frequent updates. Right now, Google Assistant's use of Slices does not call this AFAIK. – AdamK Jun 27 '19 at 03:04
  • So is it expected to come in future update or will it remain the way it is (i.e. Assistant won't call onSlicepinned() frequently)? – Ashish Rana Jun 27 '19 at 05:12
  • Sorry I don't know if or when it might be used by Assistant. I guess it will depend on if there is a good use-case for using that kind of functionality. – AdamK Jun 27 '19 at 05:20