PASSING DATA BETWEEN ACTIVITIES - Android Fundamentals

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] and guys welcome back to your new video this video will be about how to pass data to an intent to retrieve that data from another activity I want to create a layout with three edit text where you can enter your name age and country and then I would like to start a new activity and send this data to the new activity so we are able to deal with it there so right here I'm in the activity main.xml file and we will start with creating the layout let's create an edit text here set the layout width to match parent and the right layout height to wrap content then I will set the hint to name because this will be the edit text for the name I will set the idea of that edit text to et name and then I will close that tag off then we can copy that paste it two times below one time for the age of ETH and change the hint to and one time for the country so eg country and finally the button to start the new activity and sent the data that is inside of those edit text to our next activity so layered with for that button is rep content and the layer height - let's set the text of that button to apply and close that tag off and we had we need to set an ID - so ID at + ID button apply then we can go into our design tab and set the constraints for those views let's correct them a little bit below here so we can deal with them I will start with the et name constrain it to the top and then click on those two bars and horizontally impair the same with the H constrain it to the top of that by the bottom of it he named and horizontally in parent and this one too and the button I will constraint through the bottom of eg country and to the parent right then let's quickly create a new activity go to your arm package here in my case it's tutorials right click new activity and then empty activity I will call it second activity make sure to select that generate layout file option and that the source language is Kotlin and click finish then I will go into that activity second XML file and simply add a textview that will display the data we will send to that second activity so layout width is matched parent and lowered height wrap content and the text is initially let's say second activity it doesn't really matter because we will set that text when we start that activity to something else anyway and set the idea of that to TV person for example because we are sending data for a person and make sure to select - to set the constraints in the design tab for that textview constraint horizontally in parent and vertically in parent and I will increase the text size to 230 SP that looks fine then we can jump into our main activity dot Katie file and inside of here we want to set an onclicklistener to our button apply button apply dot set onclicklistener and here we want to create an intent if you watched my last video then you know what an intent is for and that we actually need an intent to start a new activity here we need to press alt + Enter to import intent inside of that intent we need to provide the current context we can refer to that with this and we need to provide the class that we that is the activity that we want to start here in this case its second activity double colon class dot Java then we can call also on that intent object and call startactivity with it and that it refers to our just created intent object here so that is everything we made in the last video where we just started that activity but now we actually have those three edit text fields here and we want to be able to enter something in those fields and when we click on apply start that new activity and display that data in that textview in our activity second and to do that we need to attach extras to our intent object so let's go into that on clipless now right before we create that intent and Whitewell name so we can extract the name from our eg name so ET name dot text dot to string then there H is equal to e th the text there are two string dot two int because H is an integer and we first need to convert that editable text to a string so we can convert it to an integer and finally with a country which is equal to eg country dot X dot two string and now inside of that also function we can refer to that intent with it and if you write it dot then there are a lot of put extra functions so that function has a lot of different versions we can execute and if you just scroll down a little bit you may notice that each version needs the name which is a string and each version in each version you can put a value so here that value is an integer here abide for example if loads a string so when we put extras to a string a true and intend then we have to specify those extras in key value pairs that means that we need to provide a key which is that name here so we need to name our extra that we put into that intent object and we need to provide a value which is the actual value so in our case what we entered in our edit texts so let's write it dot foot extra and here we can now first specify our key which is a string and the convention for that is basically that we write it in capital letters as starting with extra underscore and then what kind of extra it is so for our name it is extra name then the next parameter is our value so in this case it's just our name because we extracted that name here from our edit text name so whatever we entered in that edit text will be put into that extra which we will name extra name so whenever we started a second activity we need we need that extra name here to extract that particular name here because it could be possible that we want to provide several strings and we want that here because we have the name and the country so Android studio somehow needs a way to differentiate between them and it does that by giving them unique names which is in this case there's extra name here so let's do this for our agent country to write it dot put extra this one I will call extra H and I will put the H in here and finally it dot put extra extra country and put the country in there so that's everything we need to do in our main activity now we need to go into our second activity Katie file and extract those extras that we put into that intent object with which we started that second activity so let's click on second activity Katie and here we can create a valve name so we want to save that name variable that we put into that intent object in that variable name here and set it equal to intent which is the intent we started that activity with dot get string extra so we want to get a string extra and inside of those parentheses we now need to provide the name that we provided in our main activity for that name extra here so that is extra name we can copy that and paste it here as a string and if there is a function to get a string extra then there is also a function to get in it into extra so well H is equal to int and dot get into extra and here we need to provide the name and a default value and why don't we need to provide that default value first string extra so that default value basically means if something went wrong with that intent or if that extra here that we provide that name does not even exist so if we would why anything in here then Android so you wouldn't know what it should return in that function and in that case it would just return now and that has something to do with Java because those are functions that are implemented in Java and if we take a look with control Q then we get the documentation of that function and you can see that returns an integer and in Java and integer is not nullable but in Kotlin it is but because this is still a function that was defined in Java we need to provide the default value because for Strings you can return null in Java but for integers you cannot and in Kotlin you could even make an implementation of that get in the extra function that does not need a default value because in Kotlin you can make nullable integers but it's just not up to date here so we need to first provide that extra age and just enter 0 for example so if that extra age does not exist then our age variable will be set to 0 and finally create a verbal for the country and set it to intend getstring extra extra country and then we can create that string that we will set to our textview percent string is equal to name is age years old and lives in country and finally right TV person dot text is equal to person string and then we can run that app so let's enter some values here let's say mr. hoop is 21 years old and lives in China hit apply then you can see a next activity started and we set the textview to exactly that data that we entered in the previous activity so that is a really cool way to pass data between activities but let's say we want to transfer classes between activities in that case when I just want to demonstrate that to you that we create a class person here so go to your package new cotton file or class make sure to select class here and call it person I will make that class a data class if you know don't know what a data class is that is just to tell Kotlin that the only purpose of this class is to hold data and rule for example automatically extend that class with a generated to string functions so we can easily print that we can easily print a person without us needing to create such a string here so just create that class very quickly it only has a constructor because it only holds data and it has a name of course which is a string it has an H which is an integer and it has a country which is also string and if we want to be able to send this class between activities so to pass this class to an intent object we need to mark this class as serializable so that is just to tell Kotlin that we want to be able to pass this class to an object that can be transferred between activities and can be passed to an intent we have to write a colon after that closing parenthesis and right serializable make sure it is imported here and then we are done for that class let's go into our main activity dot katie file right below that line where we create that country and here we want to create that person object well person is equal to person which we just created and then we can pass the name the age and the country through the constructor and then we don't need all those put extra functions here we only needed one time because we only want to put that person into our intent object so delete all this and write code extra and if you take a look here if we click in those parentheses and press control P and call down a little bit you can see there is a version of that function that takes a name and a serializable and this is the version we want to use here because our our person class here is a serializable now so we can pass the name which is extra first and the person object we just created and if we wouldn't have this serializable here so which route if we remove this then this function will throw us an error because there is no version of that function that would take a person object because we just created it and it can't even know that we have a person object here and this is why we need to inherit from serializable here and actually serializable is not a class here that is an interface but you don't need to understand that for now so make sure you have that put extra function like this year then we can go into our second activity and remove all this and now we want to extract that person from our intent so well person is equal to intent dot gets utilizable extra and provide that extra person here then we can delete that person's string line here because as I told you the person class is a data class and that means we have a true string function that will print our person in a way very readable way so it will just print all the attributes of that person so let's delete that person string and actually this person is not a person currently because if you click on that gets utilizable extra function and press ctrl Q now you can see whoops that it returns a serializable and not a person in this case and because of that we need to cast this result to a person so we write as person and this is just to Talon words to you that we want to interpret that serializable result as a person and now we can actually access the properties of that person so for example person dot name but we want to just write person dot to string here to set the text to that person convert it to a string and if we now run this then we can create our mr. Kroop again 21 and china hit apply and you can see this is how the two string function works it tells us that is a person the name is mr. poop which is equal to 21 and the country is China so that is actually how you can pass classes between activities and passed them to an intent to use that serializable interface I hope this was helpful for you if so please leave a like and comment below also if you have any questions then don't mind asking them in the comments have a good day see you in the next video
Info
Channel: Philipp Lackner
Views: 12,867
Rating: 4.9703155 out of 5
Keywords: kotlin, android studio, android, beginner, newbie, data passing, activity, tutorial, 2020, up to date, programming, coding
Id: IWXYV1dC2FQ
Channel Id: undefined
Length: 18min 43sec (1123 seconds)
Published: Sat Feb 22 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.