Bundle size in bytes
Is there any way to know the bundle size in bytes? My point in asking this is I am saving parcelable object lists in my bundle
on onSaveInstanceState
.
I need to check if the bundle size is reached it's size limit and prevent any more data to get saved, and to prevent TransactionTooLarge
exception to occur.
I think easiest way for me is:
fun getBundleSizeInBytes(bundle : Bundle) : Int {
val parcel = Parcel.obtain()
parcel.writeValue(bundle)
val bytes = parcel.marshall()
parcel.recycle()
return bytes.size
}
Parcel class has dataSize() member, so the same result can be achieved without calling marshall():
int getBundleSizeInBytes(Bundle bundle) {
Parcel parcel = Parcel.obtain();
int size;
parcel.writeBundle(bundle);
size = parcel.dataSize();
parcel.recycle();
return size;
}