diff --git a/core/core-bundle/src/jbMain/kotlin/androidx/core/bundle/Bundle.jb.kt b/core/core-bundle/src/jbMain/kotlin/androidx/core/bundle/Bundle.jb.kt index d23de74857e40..0ff63a2920296 100644 --- a/core/core-bundle/src/jbMain/kotlin/androidx/core/bundle/Bundle.jb.kt +++ b/core/core-bundle/src/jbMain/kotlin/androidx/core/bundle/Bundle.jb.kt @@ -215,6 +215,7 @@ actual fun bundleOf(vararg pairs: Pair): Bundle = Bundle(pairs.siz is ShortArray -> putShortArray(key, value) // Reference arrays + is Array<*> -> putArray(key, value) is ArrayList<*> -> putArrayList(key, value) // Perform extra round (with copy) because of compatibility purposes. // Unlike Android, `listOf` result might be not castable to `ArrayList`. @@ -225,6 +226,24 @@ actual fun bundleOf(vararg pairs: Pair): Bundle = Bundle(pairs.siz } } +@Suppress("UNCHECKED_CAST") +private inline fun Bundle.putArray(key: String?, value: Array<*>) { + // Unlike JVM, there is no reflection available to check component type + when (value.firstOrNull()) { + is String -> putStringArray(key, value as Array?) + is CharSequence -> putCharSequenceArray(key, value as Array?) + // Narrowed alternative of Android's [putParcelableArray] + is Bundle -> putBundleArray(key, value as Array?) + else -> { + if (value.isEmpty()) { + putStringArray(key, value as Array?) + } else { + throwIllegalValueType(key, value) + } + } + } +} + @Suppress("UNCHECKED_CAST") private inline fun Bundle.putArrayList(key: String?, value: ArrayList<*>) { // Unlike JVM, there is no reflection available to check component type diff --git a/lifecycle/lifecycle-viewmodel-savedstate/src/jbMain/kotlin/androidx/lifecycle/SavedStateHandle.jb.kt b/lifecycle/lifecycle-viewmodel-savedstate/src/jbMain/kotlin/androidx/lifecycle/SavedStateHandle.jb.kt index 6a38da8823ac5..69a72653a579d 100644 --- a/lifecycle/lifecycle-viewmodel-savedstate/src/jbMain/kotlin/androidx/lifecycle/SavedStateHandle.jb.kt +++ b/lifecycle/lifecycle-viewmodel-savedstate/src/jbMain/kotlin/androidx/lifecycle/SavedStateHandle.jb.kt @@ -310,6 +310,16 @@ actual class SavedStateHandle { is ShortArray -> true // Reference arrays + is Array<*> -> { + // Unlike JVM, there is no reflection available to check component type + when (value.firstOrNull()) { + is String, + is CharSequence -> true + // Narrowed alternative of Android's [putParcelableArray] + is Bundle -> true + else -> value.isEmpty() + } + } // [bundleOf] might support [List] instead of [ArrayList] in some cases. is List<*> -> { // Unlike JVM, there is no reflection available to check component type