Search code examples
androidbundle

Default value on Bundle.getString(String key)


I've just noticed that, while most of getters from a Bundle have the possibiliy of including a default value, in case the key doesn't exist in that particular bundle instance, getString 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 Bundle class).

As an example, right now you have only this:

bundle.getString("ITEM_TITLE");

While I would like to do:

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

Thanks!


Solution

  • 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;
    }