java – Bundle class why is it?

Question:

PageFragment fragment = new PageFragment();
Bundle args=new Bundle();
args.putInt("num", page);
fragment.setArguments(args);
return fragment;

Question: Why Bundle needed? to create a key-value pair? or does it have more functions?

Answer:

The Bundle class (from English – convolution) is essentially a wrapper (wrapper) over the ArrayMap collection to create a more comfortable container for elements of different types in work, in which you can place any Parcelabe objects and primitive types. The class is thread safe and can be used to pass values ​​between different threads. The elements of this collection are accessed, as in ArrayMap , by key-value pairs. Additionally, the class provides methods for placing and retrieving typed data and some other capabilities.

The class is widely used to group different types of data into one entity (such as strings, primes, boolean values, etc.) for subsequent transfer both between separate methods of one object, and between different objects and even different components of the same application, mainly in cases when the types of data to be passed are not known in advance – that is, we can pass a string, or we can pass a number or something else in each individual case (in other cases, it is more convenient to use passing through a constructor or separate method arguments).

It is also convenient to use such a container to transfer the result of a method, when you need to return several values ​​of different types, and there is no particular point in creating a POJO model.

The class is widely used by the system, including for storing values ​​when changing state, but the class itself does not provide the functionality of such storage, it is only a shell for grouping data in the operation of other mechanisms.

Scroll to Top