An android activity and its life to death journey
Unknown Android
Android made its official debut in 2008. Since then we came up a long way from?android 1.0 to android 13. We have witnessed many delicious versions of Android. Let me name a few: Cupcake(1.5), Donut(1.6), Eclair (2.0), Gingerbread(2.3), Honeycomb(3.2), Icecream(4.0) and so on.
Eclair is my favorite chocolate, let me know yours in the comments
Android’s app store "Google Play Store" has over 48 billion app installs. In 2015, Android’s OS has captured 81.61% of all Smartphones sold. There were around 98.5 million Android smartphone users in the US and 2016 there are 107.7 million. Android is the most used operating system with a huge active user base of over 2.5 billion spanning over 190 countries.
So in this informative article, we'll look into the Life Cycle of an android activity. Here I tried to make the content more and more crisp but informative and easy to understand and enjoyable to read or consume than other official or non-official documentations. Now you might wonder what an activity even means in Android!!
Unbox the activity
Activity = Screen
From the above statement, we can guess what activity is in the android world. When you open an android app, every screen?you go through?is an activity. In technical terms, activity in android is like a window or surface in which the app draws its user interface, with different components in that. We have different activities for different screens in android cause?every single activity is for a single screen.
Now Let's see the Life Cycle and different phases of the life-to-death journey of an activity.
Different Callbacks in the android life cycle
OnCreate()
Whenever the user opens an activity for the first time, the OnCreate() gets invoked. It is a must-implement callback in the lifecycle of an activity cause it is responsible for firing an activity or creating an activity for the first time. It will set the content of the activity (screen). It instantiates a created state for the activity. Remember it only creates the activity. The activity is still not visible to the user.
领英推荐
OnStart()
OnStart() is the phase or callback that is responsible for making the activity visible to the user. After OnCreate() the activity enters the Started state and this is when the OnStart() callback is invoked. This callback initializes the code which is responsible to implement the User Interface of the activity. It finishes really quickly. Again to remember, Started is not the state where the app interacts with the user.
OnResume()
OnResume () is the callback that is responsible to execute the resumed state. It is the state where the activity starts to interact with the user. Now as you can see in the diagram that OnResume() gets terminated once a higher-priority activity comes in. This is when we need this callback if we need to drop some components while releasing the current activity or when we want to get back to the activity. If we need any UI component to be visible on the screen and at the same time we need to implement any functionality with that, this is the phase to implement that kinda situation.
OnPause()
The first thing to clear in this phase is that OnPause() is not equal to OnDestroy(). In this phase, the activity doesn't get destroyed at all. The OnPause() callback only removes the activity from the foreground. It might be still visible if the user is using multi-window (when we see the app history). In this phase, all the functionalities that need to perform in the foreground will be stopped. Here we can release all the functionalities that need foreground to reduce the battery consumption of our app.
OnStop()
OnStop() callback is invoked when we don't want the user to see the activity anymore. In this phase, we'll stop any kinda functionality that is running in the activity and move the activity into the stopped state when it is about to terminate. The difference between OnPause and OnStop is in OnPasue the user can still see the activity in a multi-window but it is not possible in OnStop.
OnDestroy()
OnDestroy callback is invoked when the activity is terminating or destroyed. When the activity needs to get terminated or temporarily destroyed, we invoke this callback. In this state, the activity cleans up every UI component before getting destroyed. The activity should also release all the resources it is using till now.
This is not the end of android activity lifecycle. There are a lot more detailed information about the same in Google's Andoid Developers Documentations. Go and give it a read.
If you are hanging with me till now, a big big thanks to you to read the article. Please do subscribe if you find it informative and enjoyable one to read. And make sure to give your feedback in the comments. I will give a 102% attention to your feedback and will implement that in next article.
Also Do Follow Subrata Rajak
Software Developer | React | React Native | Express
2 年Great content
Software Engineer @Simbian | React.js | Node.js | Python | Django | AI Agents
2 年Keep it up ?