Skip to content

Instantly share code, notes, and snippets.

@bapspatil
Last active October 9, 2018 05:59
Show Gist options
  • Save bapspatil/63e87696db40a5630545915225dfda78 to your computer and use it in GitHub Desktop.
Save bapspatil/63e87696db40a5630545915225dfda78 to your computer and use it in GitHub Desktop.
MySliceProvider: Implementing the onBindSlice method
override fun onBindSlice(sliceUri: Uri): Slice? {
// (1) Create a Context variable.
val context = context ?: return null
// (2) Create the SliceAction to handle clicks on the Slice.
val activityAction = createActivityAction() ?: return null
// (3) Handle the Uris for returning the Slices here.
return if (sliceUri.path == "/hello") {
// Uri's path is recognized as "hello".
// Note: ANR and StrictMode are enforced here so don't do any heavy operations.
// Only bind data that is currently available in memory.
// (3.1) Create a List with Kotlin's DSL as follows, passing in the context, sliceUri and ListBuilder.INFINITY as parameters.
list(context, sliceUri, ListBuilder.INFINITY) {
// (3.2) Add a Row to the List as follows, again using Kotlin DSL. ;-)
row {
// Set the title of row for the Slice.
title = "Hello world!"
// Set the SliceAction for when the Slice is clicked.
primaryAction = activityAction
}
}
} else {
// Error: Uri's path not found, show a Slice
// (4) Show an error Slice
list(context, sliceUri, ListBuilder.INFINITY) {
row {
title = "URI not found."
primaryAction = activityAction
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment