Last active
October 31, 2019 05:43
-
-
Save ralphgabrielle/3a5a19f9c83693a708bd6ab4e00f7732 to your computer and use it in GitHub Desktop.
Intent Extension (Kotlin)
This file contains 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 fillIntentWithParams( | |
intent: Intent, | |
params: Array<out Pair<String, Any?>> | |
) { | |
params.forEach { | |
when (val value = it.second) { | |
is String -> intent.putExtra(it.first, value) | |
is Int -> intent.putExtra(it.first, value) | |
is Double -> intent.putExtra(it.first, value) | |
else -> throw Exception("Invalid intent parameter type for : ${it.first}") | |
} | |
} | |
} | |
/* | |
Use it with : | |
fillIntentWithParams(intent, | |
"first" to 1, "second" to 2, "third" to "3" | |
) | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment