I am confused about when to use PersistableBundle class instead of Bundle class. What is the advantage of using it? Could anyone clarify it?
It is exactly what it says it is.
A mapping from String keys to values of various types. The set of types supported by this class is purposefully restricted to simple objects that can safely be persisted to and restored from disk.
You can put just about anything in a regular Bundle. A PersistableBundle however only accepts certain types:
public static boolean isValidType(Object value) {
return (value instanceof Integer) || (value instanceof Long) ||
(value instanceof Double) || (value instanceof String) ||
(value instanceof int[]) || (value instanceof long[]) ||
(value instanceof double[]) || (value instanceof String[]) ||
(value instanceof PersistableBundle) || (value == null) ||
(value instanceof Boolean) || (value instanceof boolean[]);
}
This restriction is there in order to make it persistable. Considering a regular Bundle can contain all kinds of (custom) data, it can be complex to persist that data to disk. For PersistableBundle this is easier, because you know that it simply can't contain such complex data.
Simply put, PersistableBundle is a type of bundle that is disk safe. PersistableBundle and Bundle both extend the BaseBundle class. PersistableBundle class is considered disk safe because it restricts the object types to really simple ones unlike the Bundle class. It's useful when working with Job Services and Job Schedulers. You can check the android documentation here
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With