Last active
October 9, 2018 05:59
-
-
Save bapspatil/63e87696db40a5630545915225dfda78 to your computer and use it in GitHub Desktop.
MySliceProvider: Implementing the onBindSlice method
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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