Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bundle vs PersistableBundle in Android

Tags:

android

I am confused about when to use PersistableBundle class instead of Bundle class. What is the advantage of using it? Could anyone clarify it?

like image 493
koti Avatar asked Feb 19 '18 13:02

koti


2 Answers

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.

like image 103
Tim Avatar answered Sep 20 '22 11:09

Tim


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

like image 22
Xpacer Avatar answered Sep 21 '22 11:09

Xpacer