Skip to content

Instantly share code, notes, and snippets.

@jimmyFlash
Last active December 27, 2019 11:49
Show Gist options
  • Save jimmyFlash/29f4fc0fd500141f004f978a98a2a8a5 to your computer and use it in GitHub Desktop.
Save jimmyFlash/29f4fc0fd500141f004f978a98a2a8a5 to your computer and use it in GitHub Desktop.
Extension function that tries to put a value of arbitrary type to the bundle, and throws an exception if the type is not supported. thanks to (Dmitry Akishin)
fun <T> Bundle.put(key: String, value: T) {
when (value) {
is Boolean -> putBoolean(key, value)
is String -> putString(key, value)
is Int -> putInt(key, value)
is Short -> putShort(key, value)
is Long -> putLong(key, value)
is Byte -> putByte(key, value)
is ByteArray -> putByteArray(key, value)
is Char -> putChar(key, value)
is CharArray -> putCharArray(key, value)
is CharSequence -> putCharSequence(key, value)
is Float -> putFloat(key, value)
is Bundle -> putBundle(key, value)
is Parcelable -> putParcelable(key, value)
is Serializable -> putSerializable(key, value)
else -> throw IllegalStateException("$key is not a supported type")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment