hey guys and welcome back to a new video and also a new playlist which is called Android Basics with this new playlist I want to focus on explaining the core Android Concepts which are purely used for Native development and this is also not only targeted towards beginners but also if you're already familiar with some Android development that is also for you since all these basic Android Concepts such as activities the back stack and all these kinds of things are often only scratched on the surface but it definitely makes sense to go into these a bit deeper so this playlist is not intended to make your Android developer from nothing but rather for those people who are new to the Android ecosystem or just want to deepen their knowledge but on the one hand it assumes that you know kotlin the programming language we use for Native Android development and I also won't cover any UI building here because for that I have my separate jetpick compose playlist on this playlist I will talk about things like activities the life cycle the back stack broadcast receiver Services intense all these Android specific things what I will leave out though are Concepts that don't find much application nowadays is due to Jetpack compose so for example fragments is a topic where you have an older video about but I decided to not make a video about that in this playlist because most new Android apps nowadays just don't use that anymore due to Jetpack compose and in this first video I want to talk about activities and their life cycle so whenever you create a new Android Studio project then you will end up with something like this you will have a class main activity which is in this case a component activity and another studio will generate an oncreate function which you override here with some initial content in this case with jetpack compose we want to ignore this Java composter here completely and focus on this outer thing this activity and what that actually is an activity itself is kind of a container for one or multiple screens in your app but that's only part of the truth I wouldn't only see it as a container for screens but rather as a unit of your app where your users interact with and that is also why activities are called activities and not something like screen container so this activity will contain information about if it's currently active on the screen if it's in the background it will also serve as an entry point for your app so that means if a user comes to your app from another app for example your browser um if they click on a link or so which opens you Europe then an activity is a component that directly gets launched from that action and in the past we usually did it that way that we had one activity per screen when things changed a bit and we got fragments so we often bundled the different related screens into one activity so you might have one profile activity with a normal profile screen a profile edit screen so multiple screens are just bundled into one activity which hosts these different screens and then nowadays we got Japan composed which usually means we will only have a single activity in our whole app so in this case with the jetpack compose setup here we can have lots of screens as many as we want while staying in the single main activity so in pure Japan composed products this main activity will just serve as the entry point for our application so if another component another app for example a system component needs to launch our app then it needs to directly say I want to launch this activity or I want to just launch this app and this is the starting activity of that but the main characteristic of activities in Android is the so-called lifecycle so that means at some point our activity is born so it's called created and that is what this oncreate function stands for and at some point it's not needed anymore all its memory resources are freed up and it's destroyed and in between there are just some steps that happen which move this activity into different so-called lifecycle States if you just take a look at the official activity lifecycle diagram from the Android documentation we get to see this picture this is the whole lifecycle looks super complex but let's break it down because once you understand it it's actually fairly simple as we already saw once the activity is created on creators call so it will move into the created lifecycle State and that is just the place where we want to initialize our variables inside of that activity where we want to set the actual views so the actual UI content of that activity and just do some further initialization at this point the user does not see anything on the screen yet after the created state it will start to move into the style started state so onstart will be called when that is complete an activity is considered to be startled when it becomes visible for the user but at this point the user still can't interact with it yet so you can kind of compare this with a theater where the curtains open up initially and you can already see all the actors and everything from the play but the play itself is not starting yet however after the starter state it will move into the resume State when an activity is in the resume state that means it's in the foreground the user can interact with it that's also where it will stay until the user eventually moves away so you can see here the activity is running in this case so it will block the state until something is happening as you can see as soon as another activity comes into the foreground the life cycle state will move to on pause and this is actually a bit inaccurate because it's not only moving to the pause State when another activity comes into the foreground but when any different piece of UI comes into the foreground for example a dialog which doesn't need to be an activity so at this point all the resources the activity needs will still be kept in memory because the user might still come back to the activity which is very likely if they see a dialogue or so but they just need to apply some kind of action but it could also be that they just navigate through different screen of their app where they might decide to click the back button and come back to the previous activity and if that happens so if the actual activity which is in the power State comes back into the foreground then you can see when the user returns to the activity the lifecycle will move back to the resume state so it's in the foreground just as before and it will stay there until the user moves away again but how do we now get from on pause to on stop you can see the activity is no longer visible but isn't this the same as moving away well if the user navigates from one activity to another activity then on stop will also be called and the lifecycle state will move to the stop state so what's done the difference between the stop State and the pause state if an activity is in the stop State you can be sure that it's not visible to the user anymore if it's in the past there it could still be that the user is seeing a dialogue and the actual activity that is paused is still somehow in the background so you can imagine just like on your desktop PC where you have different windows so different activities and your browser might be in the background and you might have a smaller window in the foreground which is your focus so in that case the browser which is in the background and not focused would be in the past State while the smaller window that is in the foreground is in the resume State since it has the user's Focus but as soon as the whole window is not visible anymore it would be considered stopped so that would be the case if the user navigates to another screen since then only that new screen is visible on the screen and not the previous screen anymore if the user then comes back when the activity is in the stopped State you can see then on restart this call which is kind of a special function when the user comes back to an app that was minimized before and that was hidden so this just gives you another callback to react to exactly this kind of use case and after that onstart will be called again so the activity will be made visible to the user and then on resume where the user can finally interact with it again but last but not least as you saw we also have on Destroy so what does need to happen that our activity really gets destroyed and all its resources get freed up so this diagram says the activity is finishing or being destroyed by the system what does finishing really mean so usually that either means that you either intentionally closed it with the Finish function inside of an activity or the user is currently on that activity and is active and then they click the back button because that tells the Android system a I don't want to be active on this activity anymore please take me to the previous one so the current activity is not needed anymore and it will be destroyed or obviously as this description here says if the system so the actual Android operating system needs memory and it might decide to kill your app than on Destroy will also be invoked there's also another special case in which the lifecycle will reach the destroyed State and that our so-called configuration changes that means when some kind of global configuration changes the most common example is a screen rotation so when the user rotates the device that means the layout the activity is showing also needs to be re-inflated it again needs to be loaded from the resources maybe or maybe there's a specific landscape layout so everything will be set up from the very beginning again and in that case the whole activity will be recreated and the lifecycle will start from oncreate again as soon as the device is in landscape mode then that doesn't only happen with screen rotations but also for example when switching the language of the device and many more but I don't want to go that deep into these configuration changes in this video I will talk about that in another one but they come with more problems than you might think at first and if we now take a look back here in code then for all of these lifecycle States we have a function inside of an activity we can override to react to this so if we take a look here we can also override on start which is called as soon as the activity becomes invisible as I said so here we could simply print some thing um let's say on start after on start we would call on resume which is called as soon as the user can interact with the activity so here we say print line on resume we want to have one for one pause print line on pause and let's have one for on stop print line on stop one for destroy here we want to print something and what is the missing we're missing on restart which is called when the user comes back to the app which was previously in the background so here we say print line on restart and let's also add such a log in on create since we're missing that right now and if we then change this to uncreate and then launch this app on our emulator and then also take a look in a logcat to see our logs let's just filter for these parentheses or better we replace this with system out so now we see all these functions that get called and that is exactly what you just saw from the diagram I showed you initially on Creator's code when that is done on Stardust code and when that is done so when the activity is visible to the user then the lifecycle state will move in on resume and if we take a look here on our emulator this is currently in active activity we see the screen we see this hello Android text so the user is currently interacting with the app and that is why the state stays and resumed if we now move away so we minimize the app for example um completely minimize it then you will see on pause and unstop is called on pause because it moves into the background and on stop because it completely gets invisible to the user as you can see that's the case the user does not see anything of the activity anymore and it's not just seeing a dialogue or so what the activity is still in the background and if we then get back to the activity right here and then you can see on restart is called start and resume so that was exactly what I showed you here after on stop user comes back to the activity on restart is called onstard is called on resumes called and that is where it will stay again until the user does something moves away or closes the app let's also quickly take a look at that when the user closes the app by just swiping it away then you will see on pauses chord on stop is called an on Destroy is called so then the activity is really destroyed all the resources all the UI is freed up it's not kept in memory anymore and to get it back on the screen we would need to create a new one there are certain cases where on stop and on this trial might not be called so the Android system does not guarantee that these are always cold so if you have some very important data to say for example before the user leaves the app I would always do that in on pause but other than that that is how activities and the life cycle works if you enjoyed this video then definitely leave a subscribe and don't miss the next Android basic videos you'll get two new videos here every single week Wednesday and Sunday so definitely stay tuned I wish you an amazing rest of your week and I'll see you back in the next video bye bye thank you