The Beautiful world of Activities

Activities are the basic components of any Android app. You may ask what exactly is an activity in android? By definition:
An activity represents a single screen with a user interface just like window or frame of Java.
By digging deeper into this definition, we find that it is what helps us draw the user interface. There can be multiple activities, but always only one "Main" or launching activity which is the one the user interacts with when the app is launched for the first time. Activities basically follow state transition patterns, which basically means it goes through a series of steps right from initialization until it is stopped. In very basic layman terms, you can consider an activity lifecycle to be metaphorically similar to a vendor.
As per a vendor's business lifecycle, say to start a business a vendor needs to create the objects of the trade, followed by actually starting to setup the objects of the trade ,then opening up his store for business, followed by ongoing transactions, followed by taking breaks in between when necessary, then returning back to business, followed by stopping for the day, then returning the following day and when the business doesn't work out selling out the store and shutting down for good.

Similarly, an activity is the vendor in this case. You need to first create the activity. This is done in the onCreate() method, where you initialize the essential components of your activity (say objects of trade). That is followed by onStart() method where these objects become visible (opening up the store where these objects are about to become visible to clients) , then you get into the flow where the activity has become visible, which is done through the onResume() method( in this case the objects that are being sold come under focus of the clients), followed by the activity running phase, where the activity is in the running state (our vendor is in business!) , followed by onPause where the activity loses focus (usually due to another activity taking over , in our case analogous to taking breaks) , followed by onStop where the activity is no longer visible( shutting down the store), but can always be restarted as and when required, in which case it goes back to onStart() method (aka opening the store), followed by onDestroy method where the activity gets destroyed( out of business!).

In this way, the entire lifetime of an activity revolves between the call to onCreate() and the onDestroy() states, whereas the visible lifetime starts between the onStart() and onStop() states.
Also, the foreground lifetime of an activity basically happens between the onResume() and onPause() states. The diagram below illustrates the activity lifecycle which we just discussed in brief detail:


There are a ton of processes and commands involved in between the activity-user interaction paradigm, but I'll cover that eventually in subsequent topics. As a final note I would like to state that you don't need to implement all these callback states ,just the ones which ensure the app -user interaction is carried out in the expected way. I will be covering more ground on all the activity related commands for interactions, co-ordination and transitional states as I move along. For now, just know that Activities form the base of the app and are a very essential component to understand to make sure your app works as expected without any issues. 


0 comments:

Followers

Vishwan Aranha. Powered by Blogger.