What is the life cycle of an Android activity?
In android sdk framework, Every android Activity(Window) having lifecycle methods. That means, when user enter into an application, he can see the Activity thats been created in onCreate() lifecycle method. Layouts attached in the window in onCreate() method only.
Activity(Window) has following lifecycle states:
Create - Activity is created.
Start - Current activity gets started.
Resume - Current activity has been in resumed state.
Restart - Current activity has been in restarted.
Pause - Current activity has been in Paused state.
Stop - Current activity has been in stopped state.
destroy - Current activity has been in destroyed state.
Why are so many similar sounding methods (onCreate(), onStart(),
onResume()) called during initialization, and so many others
(onPause(), onStop(), onDestroy()) called at the end?
First time user enter into an application:
When opening the application, we can see one Window(Activity). onCreate (created) -> onStart(started) -> onResume(resume state) will be called.
Close the application from background:
when closing the application from the background, activity has to be destroyed to free up some memory. So, onPause -> onStop -> onDestroy methods will be called.
When are these methods called, and how should they be used properly?
Starts the Application:
When user enter into an activity or application for the first time:
onCreate()
onStart()
onResume()
When you run the app from android studio:
onCreate()
onStart()
onResume()
Activity Transition:
When moving from First Activity -> Second Activity:
first_activity : onPause()
second_activity : onCreate()
second_activity : onStart()
second_activity : onResume()
first_activity : onStop()
When moving from Second Activity -> First Activity:
second_activity : onPause()
first_activity : onRestart()
first_activity : onStart()
first_activity : onResume()
second_activity : onStop()
second_activity : onDestroy()
Overview Button:
When user clicks on Overview Button (hardware third button - recent list):
onPause()
onStop()
After user Dismiss the Overview Button (or) User went to some other apps from the recent list and coming back to the Application:
onRestart()
onStart()
onResume()
Home Button:
When user clicks on Home Button:
onPause()
onStop()
User search the home screen and clicks on application icon to coming back to the activity:
onRestart()
onStart()
onResume()
User gets Phone Call:
When user in an Activity, phone call came up:
onPause()
onStop()
If User doesn't attend the call, it disconnect automatically and back to activity (missed call):
onRestart()
onStart()
onResume()
If User doesn't attends the call:
N/A - No lifecycle will be called.
Power off Button:
When User power off the button:
onPause()
onStop()
When unlocks the device:
onRestart()
onStart()
onResume()
Pop Up Dialog:
When pop up dialog came up - No lifecycle will be called
Restart Device or Switch off:
When user restarts or switch off device:
onPause()
onStop()
When user clicks on app icon from the home screen:
onCreate()
onStart()
onResume()