Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save EmmanuelGuther/782054f6e0896ec362e91d88287294f6 to your computer and use it in GitHub Desktop.

Select an option

Save EmmanuelGuther/782054f6e0896ec362e91d88287294f6 to your computer and use it in GitHub Desktop.
Kotlin Higher-Order Function -- pass a function as a parameter that in turn receives a parameter
fun launchEmail(activity: FragmentActivity, subject: String, text: String) {
val i = Intent(Intent.ACTION_SEND)
i.type = "message/rfc822"
i.putExtra(Intent.EXTRA_SUBJECT, subject)
i.putExtra(Intent.EXTRA_TEXT, text)
try {
activity.startActivity(Intent.createChooser(i, "Send mail..."))
} catch (ex: android.content.ActivityNotFoundException) {
Toast.makeText(activity, "There are no email clients installed.", Toast.LENGTH_SHORT).show()
}
}
fun foo(function: () -> Unit) {
when {
lastClickDate != null -> {
function()
}
}
}
foo { (::launchEmail)(requireActivity(), getText(R.string.app_name).toString(), SharedPreferencesHelper().loadFirebaseDeviceToken().toString()) }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment