Video: Using Parcelable – Extended version
hi guys Codetutor here and my name is Anil Deshpande in this particular video i am going to revisit Parcelable concept I already have a video on how to use the Parcelable and why we are supposed to use a Parcelable instead of serializable if you are not familiar with these things i would request you to stop this video go back to the link provided and come back here so in this video instead of concentrating on why we are supposed to use Parcelable i will continue it further and we will be mainly concentrating on how to Parcel different types of data types other than the string which we have already covered in the previous video so without much further ado let me directly get into the coding so if you have observed the previous video in that case i have already use these three string data types that is first name last name and qualification and you already know how to parcel them and in this case I will be dealing with the different types of data types like what if i want to use enumerated data type what if i want to deal with a array of strings that is a list of strings and how do I parcel a boolean data type and what if i want to parcel of custom array in this case i have created another class called as CityLivedIn which basically contains the city name and a date object and that city living also implements the Parcelable interface you can observe that this Date is being stored into the parcel by getTime, so it is be getting converted into a long datatype so you can see writeLong that is how it gets returned to the parcel and in the same way you can read it back by using readLong and converting it back to the date object how do I deal with a gender.
. in this case i have created it to be of type enumerated so let's get started with reading these different data types Assume that i have a height which is of type int that can be accessed by parcel . readInt so that will return me a int data type and if i have something like qualifications which is of a list of strings that can be got by parcel . readArrayList and you can pass on the null so it will you me the array of strings which in this case is qualifications and then I have a gender, gender is a enumerated data type so it's upto now for you to decide how do you want to convert this enumerated data type so that it becomes easily a Parcelable what I'm basically doing is I am writing it as a byte and if it is one then I'm interpreting it as male and if it is zero i'm interpreting it as a female I could have written it as a serializable also but once again what is the whole purpose of using Parcelable, to avoid using Serializable so that is why a readByte, and then if it is equal to 1 it is treated as a male enumeration type else it is female and then I have a boolean data type once again I am going to treat booleans as byte values I could have used a int also but i'm using byte because it is of much shorter length compared to int so this .
hostel preference and once again i will say parcel . read byte equal to one then i'm going to return true else it is going to be false, I am just using the ternary operator and what if i want to read a array of custom data types in this case it is a list of a City Lived In this can be done by a method called as createTypedArrayList, this . resident history is equal to parcel . createTypedArrayList and as you can see it takes a creator as the argument and what basically we have to pass is, creator of city lived in. so city lived in . creator and this is basically going to give me the array list of residense history I have only shown you how to read it now we will write the complementary part which is actually writing it so in case of height parcel. writeInt this .
height that is going to write the height into the parcel next is the qualifications qualifications is a array list of strings and that can be written with the method writeList so parcel . write list and pass on the list that you want to write to the parcel, I now need to write the gender I have one second interprete one being male and zero being female or anything other than one being a female so for that parcel . write byte this . gender if it is equal to male then i am going to interpret it as one and if it is zero it is going to be female, hostel preference which is of boolean type that is pretty easy once again i will be writing write byte and this . hostel preference if it is equal to true then byte value one else it is going to be byte value zero now comes the remaining part that is residense history, that can be written using a method called as write typed list, so parcel . write typed list and pass on the array list that you want to store I will run this particular code in the debug mode in the second activity i have created a break .
so that i can pause it here and show the value of the person that I am passing from my first activity which is main activity you can see that i am basically setting all different values for here and in the second activity what I will get is the person back I can see that it is get parcelable extra, so let me run it in the debug mode so let me click on this particular button and as you can see because i have put a break point here the application will pause, I just want to check what is the value of the expression of the person it is exactly the values that I have sent from the main activity so I had sent of male there one so it gets interpreted as parcelable byte value 1 and the height is six I have sent the value of height as 6 last name qualification these are string data types, its a a array list of string, qualifications resident history which are once again array of city lived in objects so you can see the city & date which are once again a parcelable object so it is a array of parcelable object that's how you basically parcel different types of data types in android that brings us to the end of this particular video if you have any questions please post them in the comments section below subscribe to the channel and let me know any new topics that we would be interested in so that i can pick up that particular topic for the next video,take care bye.