Print the contents of a Bundle to Logcat?

后端 未结 10 663
孤独总比滥情好
孤独总比滥情好 2020-12-24 04:47

Is there an easy way to print the contents of a Bundle to Logcat if you can\'t remember the names of all the keys (even being able to print just the key names w

10条回答
  •  臣服心动
    2020-12-24 05:15

    In Kotlin, recursive for when it contains child bundles:

    /**
     * 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()) {
                val value = this[key]
                when (value) {
                    is Bundle -> value.printDebugLog(key)
                    is Array<*> -> Log.d("printDebugLog", "$parentKey.$key : ${value.joinToString()}")
                    else -> Log.d("printDebugLog", "$parentKey.$key : $value")
                }
            }
        }
    }
    

    Usage: myBundle.printDebugLog()

提交回复
热议问题