Last active
February 21, 2020 21:05
-
-
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
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
| 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