Víctor Penela Víctor Penela - 10 months ago 85
Android Question

Default value on Bundle.getString(String key)

I've just noticed that, while most of getters from a

have the possibiliy of including a default value, in case the key doesn't exist in that particular bundle instance,
does not have that possibility, returning null if that case.

Any ideas on why is that and if there is some way of easy solution to that (by easy I mean not having to check each individual value or extending the

As an example, right now you have only this:


While I would like to do:

bundle.getString("ITEM_TITLE","Unknown Title");


Answer Source

You'll have to wrap it yourself:

public String getBundleString(Bundle b, String key, String def)
    String value = b.getString(key);
    if (value == null)
        value = def;
    return value;