I am totally new to Android and I'm just learning about the Activity life cycle.
In all the apps that I have made so far to practice, I hadn't been using the onStart()
method (mostly because I didn't know of it) and the apps worked perfectly fine.
Why though did they work perfectly fine?
And when do I have to explicitly write an onStart()
method in my app then?
On create
Is called when the activity is created and then never called again. Unless you open the activity again.
On start
Is called when the activity is created and also called again every time the activity is resumed (if you return to it using back button).
example
lets say we want to show a toast message we will call it "message".
First case
if we want to show "message" only when we create the activity we add the toast in Oncreate, and this is what happens
If you open activity A ----> Oncreate will be called -----> "message" is shown-----> Onstart called -----> nothing happens
If you open from A another activity B and press back ----> onCreate is ignored -----> onStart is triggered -----> nothing happens.
((SO MESSAGE IS SHOWN ONLY ONCE WHEN YOU CREATE THE ACTIVITY)).
Second case
If we want to show "message" each time activity is shown or everytime it becomes visible, we add the toast in onStart, and this what happens:
If you open activity A ----> Oncreate will be called -----> nothing happens-----> Onstart called -----> "message" shown
If you open from A another activity B and press back ----> onCreate is ignored -----> onStart is triggered -----> "message" shown again.
((SO HERE MESSAGE IS SHOWN WHEN WE CREATE ACTIVITY AND WHEN WE GO BACK TO IT)).
This is why on start is not always important to use for app to function.