Activity Lifecycle Changes: Events
Hello Android Devs, In this article I am going to explain about activity state / lifecycle changes due to different user or system events.
Lets Go…….!
Introduction
Some user triggered or system triggered events like screen rotation, switching to multi window mode etc., can cause activity to change from one state to another. During these events the activity may go through different lifecycle states.
So let’s discuss some of the events that occur and how it will affect the activity state.
1〉 Configuration Change
There are number of reasons for configuration change to occur. Some of the common examples are Screen rotation, Changing device language, Switching to multi window mode etc,.
What happens to activity when configuration change occurs?. The activity will be destroyed and a new instance of same activity will be created.
During configuration change all the views which has an ID will retain it’s state internally and views in new instance of the activity contains the previous view state. For example when you enter some text in edittext which has an ID and rotate the screen the text entered in the edittext will be retained.
The activity will go through the following lifecycle during configuration change
onPause() ➟ onStop() ➟ onSaveInstanceState() ➟ onDestroy() on Original activity instance
onCreate() ➟ onStart() ➟ onRestoreInstanceState() ➟ onResume() on new activity instance.
All the view which has an ID will preserve it’s state during configuration change but if you want to save additional data, use onSaveInstanceState, ViewModels and persistent local storage like shared preferences, Room DB etc., to preserve the data. As a developer, you can any of these or combination of these options to preserve data according to your need.
2〉 User Taps Back Button
If the user is currently interacting with the activity and presses the back button then, the activity will be destroyed permanently by the android system.
In this case saving activity state is not required because back button press is considered as normal behavior i.e, user permanently want to destroy the current instance of activity because he don’t want to return to the same instance of the activity.
The activity will go through the following lifecycle during back button press
onPause() ➟ onStop() ➟ onDestroy()
onSaveInstanceState() is not called because user don’t want to return to the same instance of the activity, so no need to save activity state.
When user taps on back button, it will trigger one additional callback method , onBackPressed(). Here you can write code to perform any logic, when back button is pressed.
3〉 Activity or dialog appears in foreground
As the name says, we are going to discuss what happens when an activity or dialog appears on top of the current activity. We can divide this case into two scenarios,
Get Shobhith J B’s stories in your inbox
Join Medium for free to get updates from this writer.
Scenario 1 : Activity or dialog partially covers current activity
If the user interacting with the current activity and opens any dialog or activity which covers the current activity partially then, the current activity will lose focus and new activity or dialog will gain focus. i.e, the current activity will be still visible partially to the user but the user can’t interact with it. In this case the current activity will go though below lifecycle callbacks,
onPause()
Whenever new activity or dialog is closed, current activity will gain focus and come to foreground. In this case the current activity will go though below lifecycle callbacks,
onResume()
Scenario 2: Activity or dialog completely covers current activity
If the user interacting with the current activity and opens any dialog or activity which covers the current activity completely then, the current activity will lose focus and new activity or dialog will gain focus. i.e, the current activity will not be visible to the user at all. In this case the current activity will go though below lifecycle callbacks,
onPause() ➟ onStop()
Whenever new activity or dialog is closed, current activity will gain focus and come to foreground. In this case the current activity will go though below lifecycle callbacks,
onRestart() ➟ onStart() ➟ onResume()
When user taps on Home Button in mobile phone, the activity will fall under scenario 2 .i.e, Current activity behaves exactly like ”Scenario 2: Activity or dialog completely covers current activity ”.
4〉 System kills app process
The system kills an app process to free up the memory and allocate this free memory to other app process which has higher priority than currently running app.
You may have noticed that, you opened instagram and after using it for few minutes you pressed home button and then opened whatsapp. After spending few minutes in whatsapp , if you again open instagram from recents screen then sometimes it will open at the exact last screen or last post that you have visited but sometimes it will open from beginning, as your opening a closed app.
This is because the instagram app process will be kept when there is enough memory for whatsapp to run smoothly. If there is additional memory requirement then instagram process will be killed.
The priority of killing app process is determined by the android system based on the state of that process. The priority of the process in turn depends on the priority of the activity.
After deciding which activity or process has less priority, the android system will kill the entire app process instead of just killing the activity. So, every components like activity, service, receivers will be killed which were running in the app process.
For more information visit the below links,
https://developer.android.com/guide/components/activities/state-changes
https://developer.android.com/guide/components/processes-and-threads
https://developer.android.com/guide/components/activities/activity-lifecycle
That’s it from my side. Thank you for reading this article! Happy learning 😊










