bundle - Parceling objects in android to transfer from one activity to another -
recently interviewer asked me tricky question. there several parts of question.
- why (question why , not how) need parcel objects while sending 1 activity , not send directly
answer gave -
parcelable gives capability developers restrict object creation in way makes faster use.
i confused on part, decided site difference between using serializable , parcelable :p (clever huuuhhh !), http://www.developerphil.com/parcelable-vs-serializable/ used reference.
- while using bundle, when use string, int not need parcel data, think string/int default internally parcelled ?
answer gave -
because string/int primitive data-type, if had used wrapper class directly, might possible had use parcelable(i not sure on part)
i did not useful link after googling, or interviewer not quite satisfied answer. if guys can help, wonderful !
why (question why , not how) need parcel objects while sending 1 activity , not send directly
parcelling/serializing objects isn't speed had guessed.
when you're sending data between activities, , between different applications (remember intent
objects aren't meant communication between own activities, between yours , of other apps well), cannot expect sender , receiver have access same memory address spaces.
android's documentation states applications run in own discrete memory spaces. here's quote effect docs:
each process has own virtual machine (vm), app's code runs in isolation other apps.
so when want send object myobject
receiving activity
, can't send reference/pointer because receiver won't have access location specified pointer. instead you'll have send representation of myobject
receiver can access , use -- why need marshall data form can unmarshalled, , easiest way have class of object implement serializable
lets java best convert object array of bytes can sent , unmarshalled receiver. since serializable
uses reflection, slow.
you can use other ways faster marshall data -- one, example, converting object json
representation using library gson
, sending across since json
document can represented string
, converted java object
. way, faster in pretty cases using parcelable
interface lets specify exactly how want marshall data , exactly how should unmarshalled. gives more control on transmission of object.
the tl:dr: parcelling/serializing etc used because can't send memory addresses across, have send actual data of object , has represented in form.
while using bundle, when use string, int not need parcel data, think string/int default internally parcelled ?
how bundle
works internally puts map
, parcels/unparcels data needed (ie when get/put called). putting objects bundle
, object's class needs implement serializable
or parcelable
because needs tell bundle
how should marshalled/unmarshalled internally.
but primitive types , strings simple enough , used enough developer doesn't need specify how needs happen , bundle
provides convenience methods it. can't give solid answer @ lowest level of how works because lot of parcel
code natively implemented , couldn't find online, must straightforward convert representation in bytes.
Comments
Post a Comment