开发者

How do I know what data is given in a Bundle?

I'm having a heck of a time figuring out what data is coming to my methods through Intent/Bundles. I've tried adding break points to inspect the data, but I don't see anything. Perhaps because it's a Parcelable I can't manually read it in Eclipse.

For instance, a onActivityResult(int requestCode, int resultCo开发者_如何学JAVAde, Intent data) for a Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI). How do I know what data is available? Notice, I'm not ask for WHAT data is available but how the heck do I figure it out so I can apply the same idea to any Bundle/Intent from the Android framework? Perhaps it's a simple as looking at the docs, but I don't see a full listing of the data and I can't see it in Eclipse. So I'm at a lost.


Bundle.keySet() gives you a list of all keys in the bundle. That said, typically you just expect certain keys and query them, but keySet() is used to examine bundles you get from somewhere.


public static String bundle2string(Bundle bundle) {
    if (bundle == null) {
        return null;
    }
    String string = "Bundle{";
    for (String key : bundle.keySet()) {
        string += " " + key + " => " + bundle.get(key) + ";";
    }
    string += " }Bundle";
    return string;
}


i getting alll key and value of bundle stored...

for (String key : bundle.keySet()) {
    string += " " + key + " => " + bundle.get(key) + ";";
}

output:

(key)       :(value)    
profile_name:abc


The only thing you get out of a Bundle is what you put in. Bundles are ways of passing information between activities. If you're in charge of your entire application, you shouldn't need to look inside the Bundle for your objects, you should just grab them. Think hashmap keys... if you don't know the key, it's not like you can search the hashmap.

To place an item into a Bundle and pass it on to the next activity, you need to put it as an Extra. Take a look here for an example of passing data via extras and bundles between activities.

Copied and pasted below:

From Activity1

Intent intent = new Intent(this,myActivity2.class);
Bundle bundle = new Bundle();
bundle.putString("myValue", myValue);
intent.putExtras(bundle);
navigation.this.startActivity(intent);

In Activity2

Bundle bundle = getIntent().getExtras();
act2MyValue= bundle.getString("myValue");
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜