PageFragment fragment = new PageFragment(); Bundle args=new Bundle(); args.putInt("num", page); fragment.setArguments(args); return fragment; Q: Why is the Bundle needed? to create a key-value bundle? or does he have more features?
PageFragment fragment = new PageFragment(); Bundle args=new Bundle(); args.putInt("num", page); fragment.setArguments(args); return fragment; Q: Why is the Bundle needed? to create a key-value bundle? or does he have more features?
Bundle required for temporary storage of data during execution. This is a great choice when transferring data between activities. This is a way to save data when changing screen orientation.
In general, this is the saved data that the system uses to restore the previous state. It is a set of key-value pairs.
The Bundle class (from English - bundle) is essentially a wrapper (wrapper) over the ArrayMap collection to create a more comfortable container for items of different types, in which you can place any Parcelabe objects and primitive types. The class is thread safe and can be used to transfer values between different threads. 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 features.
The class is widely used for grouping data of one type into one entity (such as strings, primes, Boolean values, etc.) for subsequent transfer both between separate methods of the same object and different objects and even different components of the same application mainly in cases where types of data transmission are not known - that is, we can send out to the line, and the number may or Thu something different in each case (in other cases it is more convenient to use transmission via constructor arguments or individual method ).
It is also convenient to use such a container to transfer the result of the method when you need to return several values of different types, and creating a POJO model does not make much sense.
The class is widely used by the system, including for storing values when a state changes, but the class itself does not provide the functionality of such preservation, it is only a shell for grouping data in the work of other mechanisms.
The bundle class is used to transfer data of simple types (boolean byteshort int long char float double) between activations. The transfer is carried out through various mechanisms (1. Just a key value 2. Interface Parcelable 3. mechanism Serizilable 4. library GSON From Google.) Do not try to pass through the bundle link to an object or a pointer to an object. It is for simple types only. If anyone is interested in an example of data transfer, I can look at my simple example — an experiment on data transfer through a bundle on a githaba. https://github.com/aleontsev/TrasferingDataBetweenActivitiesParcelableSerizilableBundleandCrashalyticsSentry
Source: https://ru.stackoverflow.com/questions/623829/
All Articles