Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Android Bundle enum support using kotlin extension methods
import android.os.Bundle
// Dunno if there's a better way to extend both bundle and intents, but
// you probably can extend intents in the same way
fun Bundle.putEnum(key:String, enum: Enum<*>){
putString(key, enum.name)
}
inline fun <reified T: Enum<T>> Bundle.getEnum(key:String): T {
return enumValueOf(getString(key))
}
enum class KotlinEnum {
KOTLIN_IS_LOVE
}
class MyClass: Fragment() {
companion object {
private const val ENUM_KEY = "ENUM_KEY"
}
//... some fragment declaration code
fun newInstance(ktEnum: KotlinEnum) {
val args = Bundle()
args.putEnum(ENUM_KEY, ktEnum)
// Both way works
val cEnum = args.getEnum(ENUM_KEY) as KotlinEnum
val dEnum = args.getEnum<KotlinEnum>(ENUM_KEY)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.