Hey.
In this video we will talk about the activity a life cycle about life cycle methods and how to manage
the state of an activity using these methods.
I created a new project called it activity life cycle.
Here is the standard main activity code which is lauded by default.
And as usual there is immediately the create method don't create method is one or the activity life
cycle methods.
Main activity is in the character from that class up compound activity from which it will inherit this
method on create this is an annotation over right here.
That is weird.
Override the method of all apparent class outcome productivity.
Right here the code that we want to execute when creating our activity when it is first created for
the first time.
In addition to the create method there are several more methods of the activity lifecycle.
Let's go to the browser and type in the query Android lifecycle
and go to the developer dot Android.
That chrome site understanding or the activity lifecycle.
And here we can see information about the activity lifecycle how it all happens it also shows that scheme.
You can read about it in detail I'll try to explain my own words what happens during the life cycle.
Here is a diagram written activities launched that is at startup at the first launch of our application.
Our main activity are launched the main screen and then the create method is called this method.
Next on START and on resume methods are called immediately.
That is these three methods.
I executed sequentially and it says activity running.
Then it says another activity comes to the foreground.
That is that not activity is appears in the foreground.
It can be activity from our application as it was.
If you remember the music shop application that we developed when trying to send an order by email.
Now we've got a small windows selection window from which we could choose some application with which
we can send this order by email and that is this activity new activity did not completely overlap us
and our main activity went to background or it can be activated from another application not only from
ourselves.
It also can come to the foreground and our activity goes into the background and in the case when it
goes into the background the own pulse method is triggered.
Next if we go to the right the user returns to the activity.
We go to our activity and their own resume method is called.
That is we resume our activity.
If our activity is completely invisible that is it is close completely by another activity.
Then the on stop method is triggered further.
If the activity ends or is destroyed by the system then on destroy works what does the destroyed system
mean.
Android system itself can destroy all activity.
It does not depend on us.
Sometimes that is when consuming resources.
The big consuming the Android system itself choose which applications which activities it can close
destroy which processes dependent on the priority over the application.
So let's now examine this with an example.
Let's create the rest of the lifecycle methods.
So start
on resume
then on pause
on stop
and on destroy
and now let's try to see how these methods will work in our application.
We will add code that when the method starts we'll write something to look at and also displayed on
the screen.
Now let's go to the activity Main and give to all it takes.
Do you I.D.
let's just call it TextView
Max.
Let's remove the full text and make the text size larger.
For example 30s B so that it can be seen better.
Further in our main activity we will create a private field or the text.
Class
and bind it in the own create method using one view by a D
and now let's arrive first in the old create method right to the locker.
The following lock the D lifecycle method would have the key
lifecycle method.
And here we will specify the name or on create method since this recording will be made in the create
method so that we this is understood.
So we will write here and create and we will also add that the same takes to our text.
We can add takes using the append method and then the text is added not restored again but added to
Joe join to the existing one text.
Do you want and banned this text use or append and so that each time the text draws from a new line
we add the full court plus backslash and an this is the sign of a new line.
Next class copy this code and paste it into all our lifecycle methods.
Of course in the error method we will change the text to the appropriate here we will write on START
HERE own resume
et cetera
and now let's Iran our application.
So as you can see when the application starts we immediately see on grade on start an all resume.
That is when the application starts these methods of the activity lifecycle are launched in the same
way we can look and look cut into the key here
and here uh these method methods is displayed to let's experiment now.
So no activity into the background.
And as you can see on pause on stop immediately works if we return activity then on START and on a resume
is added.
That is when activity goes into the background on pause and on stop is triggered are triggered.
And then in the reverse order or start an appraisal we can do it again again on pause and stop.
So I brought another activity by accident.
Now if we go all this on site and on a resume do we understand the meaning here.
If we click on this button then on pause and stop and on destroy work the activity is destroyed.
And now if we run it again then it is created.
You see everything has been erased all from the text too and the create method was launched in which
the text was initialized and work this code and then launched on stored and all resume then the same
thing will happen again.
Everything is clear I think.
But what will happen if we change orientation from vertical to horizontal.
For example let's do and as you can see our activity was destroyed and created a new on pause on stop
on destroy and then on create on site and on a resume worked.
Now if we do everything as before then everything works again.
And if we change orientation back again again the same methods worked and activity was destroyed.
And this happens because when you change the orientation to redraw the user interface and androids to
do first completely destroys the activity and then recreate it with the on create method.
In this case we may lose some data as you see only the initial data appears and all the data that was
before this is lost.
This may be unacceptable in some situations if it is necessary that the data is saved and how to save
this data when changing orientation.
I will tell you in the next we do.