Junior — Middle
What data is permissible to include in a Bundle object during development?
sobes.tech AI
Answer from AI
In Android, an object of type Bundle is used to store and transfer data between components (Activity, Fragment, Service, etc.). You can place the following types of data in a Bundle:
- Primitive types: int, long, float, double, boolean, etc.
- Strings
- Arrays of primitive types and strings
- Lists (ArrayList) of primitive types, strings, or objects implementing Parcelable or Serializable
- Objects implementing Parcelable or Serializable interfaces
- Other Bundles (nested)
Bundle is used to package data in key-value pairs, where the key is a string, and the value is one of the supported types. For example:
Bundle bundle = new Bundle();
bundle.putString("username", "user123");
bundle.putInt("age", 30);
ArrayList<String> list = new ArrayList<>();
list.add("item1");
bundle.putStringArrayList("items", list);
It is important not to put objects into a Bundle that do not support Parcelable or Serializable, otherwise an error will occur during transfer.