‫Welcome back.
‫In this video we're going to learn about activity lifecycle.
‫The Android activity lifecycle is a crucial concept in Android app development and understanding.
‫It is essential for building robust and well-behaved Android applications.
‫The Android life cycle defines how an Android component, such as an activity or fragment, transitions
‫through various states during its existence, and in this video, we're going to discuss the activity
‫callback methods.
‫We have seven methods on create, on start or resume or restart on pause, on stop and on destroy.
‫Let's move to Android Studio.
‫Whenever you create any new project in Android Studio, the Oncreate method is created.
‫This method is called when the activity is first created.
‫You can perform one time initialization tasks here, such as setting up UI elements and data.
‫And this is what we've done before in the previous lessons, such as initializing the widgets.
‫The saved instance state parameter is used to restore the previous state of the activity, if applicable.
‫In this Oncreate method, I create a toast message dot make text just to display a toast message.
‫A short text message telling us that the Oncreate method is called.
‫We need to specify the context which is the main activity and this activity that we are on it.
‫The message Oncreate method is called and the third parameter is the length toast dot short and don't
‫miss to call show method.
‫Let's run our application and if you have this similar error, you need to update and compile against
‫34.
‫This is an issue in Android Studio.
‫I need to go to Build.gradle scripts and here I need to change the compile SDK 30 to 34 and the target
‫SDK to 34.
‫Sync now and run again.
‫And here we go.
‫The Oncreate toast message is displayed.
‫Let me run again in order to see it more visible.
‫Oncreate method is called.
‫Now let's move to another method outside the Oncreate.
‫I'll override on start.
‫So type on start and you can see it selected override function on start.
‫And this super dot on start override is a keyword in Kotlin that indicates you are overriding a method
‫from a superclass or interface.
‫Function is a short for function, and is used to define a method or function.
‫When you see override function, it means that you are implementing a method that already exists in
‫a superclass or interface, and providing your own implementation for it.
‫In the subclass, please review the inheritance in the previous sections.
‫Super dot Onstart super is a keyword in Kotlin used to call a method or function from the super class.
‫On start is a method that exists in Android Activity class, and it's one of the Android lifecycle methods.
‫When you call Super on start, you are invoking the Onstart method of the super class, which is in
‫this case the activity which ensures that the Superclass's behavior is also executed along with any
‫additional behavior you define in the overridden on start method.
‫And the additional behavior that I need to define here is to display a toast message telling us that
‫it's on start method is called let me run Again.
‫Here we go.
‫Oncreate is called and Onstart is called.
‫Then I need to override another method called Onresume.
‫This method is called when the activity is interacting with the user.
‫You can start animations, play media, and handle UI updates.
‫That should only occur when the activity is in the foreground.
‫And in the same way, I'll copy and paste this toast message and tell that we are on resume method.
‫Run again and here we go.
‫Oncreate.
‫On start and on resume methods are called.
‫The forth method is on pose.
‫This method is called when the activity is about to lose focus, but is still visible to the user.
‫You should pose or release any resources that are not needed when the activity is not in the foreground.
‫In the same way, I'll copy and paste a toast message and telling that it is on post run.
‫And here we go.
‫Oncreate is called Onstart is called, Onresume is called.
‫And when we lose the focus, you lose the focus of this app if you click on this home.
‫So on pose method is called.
‫On stop.
‫This method is called when the activity is no longer visible to the user.
‫You can release resources that should be closed when the activity is not active.
‫Another method is on restart.
‫This method is called when the activity is about to be restarted after being stopped.
‫You can perform any necessary tasks to prepare for a restart.
‫In this example, I'll use this toast message on restart run.
‫These are the methods on start and on resume.
‫If I click this home on post is called.
‫If I click this overview and then select app.
‫You can see on restart method is called and on start method is called.
‫Then on resume following this hierarchy on pause, on stop, on restart and then on start and on resume.
‫Resumed activity running in foreground.
‫The last method is on destroy.
‫This method is called when the activity is being destroyed.
‫You should release all resources Unregister receivers and perform any final clean up.
‫Understanding and properly implementing the Android lifecycle methods is a crucial for creating Android
‫applications that behave as expected and manage resources efficiently.