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 would be cool)?
相关问题
- Delete Messages from a Topic in Apache Kafka
- Jackson Deserialization not calling deserialize on
- How can I create this custom Bottom Navigation on
- How to maintain order of key-value in DataFrame sa
- StackExchange API - Deserialize Date in JSON Respo
I have developed a library called
pretty-print
which annotation processor that prints the contents of the bundle in a nice table format. Do check it out https://github.com/NULLPointerGuy/pretty-printYou can get more specific by printing the mapped value as follows:
In Kotlin, recursive for when it contains child bundles:
Usage:
myBundle.printDebugLog()
Bundle#keySet() should work.
And if you want to get the Object, you can use
Bundle#get(String key)
(which is also in the same documentation I linked at the top of my answer). However, keep in mind using the genericget()
call:toString()
will be invoked and all will be fine. However, if you actually want to use the key's pair, you need to doinstanceof
checks to avoid calling the wrong method.Realize that this isn't answering the question exactly, but I see allot of developers trying to dump the contents to logcat/console because they are not aware that they can set up in Android Studio debugger to display customized object rendering at debug time, when you hit a break point. And in the case of Bundle, you can take the type of code shown in the other answers here, and apply that as a custom renderer, so that you don't need to pipe the dump to logcat and/or the console.
(These instructions are from Android Studio 3.1.3 (June 2018) ...
Now, when you run your app, and you hit a breakpoint that shows a variable that is of type android.os.Bundle, you'll see the output generated from the above code, on the variables section of the debugger window.
I'll also include a screenshot, showing what I described above ...
Bundle-to-string converter:
Example usage:
and output:
Note that the arrows
=>
and semicolons;
let you mention spaces in the keys and values. One space before the arrow, one space after the arrow, no space before the semi-colon, one space after the semi-colon, one space after{
and one space before}
, and all other spaces are there because they are in the keys or values.