INTENTS AND STARTING ACTIVITIES - Android Fundamentals

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] hey guys welcome back to your new video this video will be about intense which is another unique concept of Android development and you will learn how to use those intends to start a new activity so I will show you how you can create a new activity class from scratch and start it there is actually a shortcut in Android studio that will allow us to do all those needed things at once for us but I think it's important that you at least once see what you need to do to add a new activity to your app so this first time we want to we will do it from scratch and then I will show you how we can actually speed that process up and create activities much easier so let's start by going into your activity main dot XML file and drag in a button because I want to start our new activity when we click on that button let's constrain that button in the center so click on those two bars here horizontally in parent and another time for vertically in parent then let's jump into our XML layout editor and change the button ID to button open activity and I'll change that text to click me then let's start to create a new layout file for our next activity by going into your app folder rest and layout and then right click on layout new and layout resource file then this dialog will open and we can choose a file name for our activity layout file and I will choose activity second because this is our second activity and this is basically the naming convention for activity layout files that you start that with activity followed by an underscore and then the name of your activity I will also set the root element to a constraint layout and then simply press ok and this is our new activity out file here I just want to drag in a text view constrain it horizontally in parent and also vertically in parent and I want to add a button that is used to go back to our first activity I will constrain this horizontally impaired to and i will constrain the top to the textview bottom and the bottom to the parent bottom then I will go into our text editor here to change the textview to welcome to second activity change the text size to 30 SP and I will change the text of that button to go back and also the ID of that button to button back the next thing we need to do is we need to create a cotton class for our just created activity so actually we didn't create an activity we just created the XML layout and now we want to create that activity by creating a cotton class just like we have it here for our main activity so to do that go into your java folder come and then open your app package and inside of that folder in my case it's the tutorials folder right click new Kotlin file or class then you can choose that you want to create a class here and our call it second activity in this case if you want to create a Kotlin class and it's an activity then the naming convention is like this that you first name it with the name of that activity followed by activity so then press ok so now we have that empty cotton class called second activity and just as we did it for our main activity or just as android studio did it for us we need to inherit from app compat activity so let's go into our second activity and tail and register you that we want to inherit from app compat activity and make sure to call that constructor and then we can go into here and right on create because we want to overwrite our on create function and there are actually two implementations of that and create function one is with a saved instance state and with persistent state we don't want that option we want that option only with saved instance state so make sure to select that one press ENTER and n-words to you will insert that function and if you take a look in that main activity then we need to call that setcontentview function here to tell in words to you what layout file belongs to our activity so let's call that function here set content view and our just created layout file has the ID our dot layout dot activities second but for now we only created that activity class but we don't start it yet from our application and we want to be able to click on that button in main activity that click Me button and when did you like likes on that button then we want to open that second activity which we just created so let's go into that oncreate function in main activity make sure its main activity now and right button open activity that set on click listener and inside of that and click listener we now can start that second activity and how are we going to do this to start an activity in Android we need an so called intend you can think of an intent as an intention to do something we can define an intent to tell the Android system we want to perform a specific action and in our case that specific action is to start our second activity and actually we can do much more useful stuff with and than just starting our activities but this is something for the next videos in this case I will define an intent like this just right intent and you probably have to import that so make sure this intent is underlined and then simply press Alt + Enter to automatically import that intent and inside of that intent object we want to create here we have several ways to do that and I will choose this one here which needs the package context and a class if you don't know what the context is then make sure to watch my last video about toasts in context in this case we just need to provide the context here which we can refer to with this because we are inside an activity class and then we need to provide the class of the activity we want to open so in our case that's second activity so we don't need to create an instance of that second activity instead we just need to refer to the class second activity and we do this by writing second activity so that the class name followed by two colons and then class dot Java so in this case we just define and intend with our current context of this main activity and we define it so that we open the second activity with it so by doing this we create an instance of an intent so we tell Android we tell our Android system that we have the intention to start our second activity but currently we only have that instance of that intent and that does nothing we need to call dot also on this to actually start an activity with it that also function is a scope function of Kotlin and when we call this then inside of that block we can refer with it to that created in 10 object here so that is just a very smooth way to start an activity in Kotlin inside of that block there is a function start activity and you can see that function takes and intent here and that is exactly the function we want to use here and as I told you we can refer with it to our intent object here so we can just write it in here and this is enough to actually start our activity so whenever we click on that button open activity we create that intent and we also start that activity with it and when we run this app now and click on click me you can see that our app crashes but why is that so we recreated in layout file for our activity second then recreated a class for our activity second or a second activity and we also set the content view activity second but we are missing one thing and if you watched my previous tutorials carefully the tutorial about my androids two-year round trip there I told you that we need to define different components of our app in the manifest so if we take a look in that manifest folder here double click on that then we actually need to define our second activity in that manifest file and we do it by going into that application tag so after that first take closes here and as you can see here is already our main activity defined and we need to define our second activity here so open the tag and write activity and Android studio will automatically suggest you to enter an activity here in our case this is second activity so just press enter here and close that tag off and when we now run that app it should work so let's now click on that click Me button and now I can see our second activity opens and currently we don't have the option to go back because we didn't provide an action for that let's actually change that go to second activity Katie and here we can add an onclicklistener to our button back button back that's set on click listener and that is a really simple function to kill our current visible activity that is just finished without any parameters that will just clear our current activity from the activity stack if you don't know what the activities that is then watch my video about activities and their lifecycle so when we now run that app again and click on click me then we can go back by clicking on go back and we can also go back by clicking on that back arrow here that has the same effect as the finished function and as I told you in the beginning there is a much shorter way to create an activity without us needing to create that separate layout file that separate activity class and to define that activity in our manifest so to actually make that much shorter we can go to our tutorials folder here right click new and you can see that Android studio has many arm templates basically here if we hold on to activity then we can create an empty activity here click on that and I will name it a third activity and you can see we can check that option that we want to generate a layout file so Android studio will automatically generate that and you can see that it automatically chooses our correct naming convention for the layout file that we write activity at the beginning followed by an underscore make sure the source language is Kotlin and then we can simply click on finish then anritsu you will create all those files that are needed it will automatically define that activity in the manifest so if we click on our android manifest you can see it inserted or a third activity here into that manifest and it also created the activity third XML file here double click on that which is just an empty layout file here so we can drag in a text view constrain it and change the text to third activity and we need to provide a button in our second activity to actually start the third one so go to activity second XML we can drag in another button here and let's make it on top of that go back button constrain it and change the text of that button to start next activity and I will change the ID to button next activity and we can go into our second activity dot Katie file to set the onclick listener for that button so button next activity dot set onclicklistener now we define our intent again we need to import it so press alt' + enter' refer to our current context with this and we want to open the third activity dot armed clasp Javari dot also and inside of that also function we write start activity with I T so with our just created intent object here and if we now run that app we can click on click me when we are in the first activity then the second activity will open and we can click on start next activity so the third activity will open and here we can click back to go back to our second activity and here we can click go back or on that back arrow to go back to our first activity so this is everything I wanted to show you in this tutorial I hope everything got clear if not then don't mind asking your questions in the comments so I can answer them if you like this video then please give it a like and comment below what you think about this and also if there's anything I can improve on that would be really helpful for me have a good day it's your next video bye bye
Info
Channel: Philipp Lackner
Views: 19,380
Rating: undefined out of 5
Keywords: kotlin, newbie, android, android studio, 2020, xml, ux, ui, intent, activity, activities, start, tutorial, programming, android development
Id: 1xj9G2FvLeE
Channel Id: undefined
Length: 16min 10sec (970 seconds)
Published: Thu Feb 20 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.