/**
* Recursively logs the contents of a [Bundle] for debugging.
*/
fun Bundle.printDebugLog(parentKey: String = "") {
if (keySet().isEmpty()) {
Log.d("printDebugLog", "$parentKey is empty")
} else {
for (key in keySet()) {
when (val value = this[key]) {
is Bundle -> value.printDebugLog(key)
is Array<*> -> Log.d("printDebugLog", "$parentKey.$key : ${value.joinToString()}")
else -> Log.d("printDebugLog", "$parentKey.$key : $value")
}
}
}
}
fun Bundle.toPrintableString(): String {
val sb = StringBuilder("{")
var isFirst = true
for (key in keySet()) {
if (!isFirst)
sb.append(',')
else
isFirst = false
when (val value = get(key)) {
is Bundle -> sb.append(key).append(':').append(value.toPrintableString())
else -> sb.append(key).append(':').append(value)
//TODO handle special cases if you wish
}
}
sb.append('}')
return sb.toString()
}
样本:
val bundle = Bundle()
bundle.putString("asd", "qwe")
bundle.putInt("zxc", 123)
Log.d("AppLog", "bundle:${bundle.toPrintableString()}")