Android Activity Explained

Nikhil Soni
Jul 20, 2017 · 6 min read

So what is an activity in android ?

I guess you all have use some sort of desktop applications and the thing common between those applications are they run inside application window.

These are the sandbox environment provided by the Operating inside which your code would run and perform the appropriate task, they also provide additional tools like maximize and close and those cannot be modified by the you as they are pre-configured by the operating system, likewise you cannot manage the creation and destruction of the application window what you can manage is what happens when its created or destroyed but not the life cycle it self.

How does this relate to android ?

Well android follows a similar concept, and here these sandbox environment are called activities, and inside those activities you can write code that will execute upon start of the activity.

Generally an application contains multiple activities and each activity can have its own set of code to perform certain operations, So how does android deal with multiple activities ? What happens to when you start another activity a while other is still running ?

All these activity creation and destruction operations is managed for you by android, you cannot alter the method or the sequence in which they happen as they are managed by android operating system, but what you can do is to perform certain task when activity enter certain state, But what do i mean by state ?

When you launch and application the starting activity is invoked and it goes through part of its life cycle methods before user can interact with the application.

And these methods are called life cycle methods and they get invoked every time a activity is started and destroyed.

These methods are :

  1. OnCreate(Bundle savedInstanceState)
  2. OnStart()
  3. OnResume()
  4. OnPause()
  5. OnStop()
  6. OnDestroy()

Lets get into the details of what each of these method does :

  1. OnCreate(Bundle savedInstanceState) : This method gets invoked as soon as the activity is launched and is responsible to inflating views to be displayed, here you should perform all operation that should happen only once for the lifetime of activity like initialization or binding data to a list, this is the only method that you need to override to run the application as it sets the view to be displayed , Overriding of other life cycle methods is optional and depends upon your use case , if you are wondering what is that savedInstanceState parameter, don’t worry it has be covered somewhere in the bottom.
  2. OnStart() : This method is responsible for making the view visible to the user, by default you don’t need to override this method to make your app run but if you want to initialize things which alter the state of ui then this should be the place where you do those initialization, Eg : Initialization of Location Manager or Broad Cast Receiver.
  3. OnResume() : This method is called after onStart and after the execution of this method finishes ,app is made available for the user to interact with.

These these three methods are guaranteed to be called in that sequence as soon as you start your activity for the first time and these methods call the next one as soon as former method has finished its execution.

You might be thinking if i just override all these three methods and do all my initialization and data binding correctly i am good to go, No!.

See the hardware devices don’t have infinite memory or storage, so any resource you are holding on should be released when not needed like if your application requires to access the device camera you should release the camera when your application goes to background or is destroyed so that other application can have proper access the camera , always try to use limited resources because your application is not the only application on your user phone and your application hogging all the memory might lead to user uninstalling your app.

The last three methods in the activity life cycle are a good place to release resources you don’t need because they get called when your application goes to background or is being destroyed by the user or due to limited availability of resources

4. OnPause() : As soon as you leave the activity this methods gets called, this might be due to user switching to other activity or due to other application taking the focus, this method is good place to release resources that clog the memory and affect the users battery life like camera or any device sensor, if an activity is in pause state the android system keeps the state of your activity intact in memory so that when your activity is resumed it wont loose its data or view so app wont have to do initialization again, this method is very short lived so don’t perform any heavy operation in it.

5. OnStop() : This method gets called when the activity is no longer visible to the user, this method should be the place where you release all the resources that are not needed like a Location Manager or Broad Cast Receiver or any other objects that you hold on to, this method is not as short lived as OnPause() hence you can perform some intense operations like database operations ,While most of things initialized is destroyed what remains intact after this method have finished its execution is the state of view.

6. OnDestroy() : This is the final call that activity receives so bye bye Activity, the activity is completely destroyed after this stage, and hence the resources that have not been released by OnStop() will be released.

Now lets talk about how these life cycle methods interact with each other, the first three methods get called as it is in that sequence and user can interact with the app, but when the activity hits the :

Pause State : If the activity is brought back to action after it hits the pause stage and only the resume method get invoked and all the initialization required upto OnStart is not needed, but if a app with high priority needs memory the system might kill the app to claim some resources for that app and if then the activity is brought back to life again it will call OnCreate and so on, since the app may hit pause and resume state frequently its not recommended to do initialization on OnResume.

Stop State : If the activity is brought back to action after it hits the stop stage only the methods from OnStart of the activity gets invoked and so on, but here also if resources are needed the app is killed and the app starts again from OnCreate if called.

Destroyed State : Its a dead end, once app has entered destroy state there is no coming back at all and all the resources that it holds upon even the view object will be released.

Next lets understand what was that “Bundle savedInstanceState” parameter in the onCreate Method, a Bundle is something which acts like a Map from java and used to pass data between activities, why do we need it here?

When you rotate your device the activity is destroyed and recreated and hence all the data that its views held is lost like a name in text field, here comes bundle to rescue when your activity begins to stop a methods called “onSaveInstanceState(Bundle savedInstanceState) ” is called you can Override this method and use the parameter savedInstanceState to save the values like name in text field and later when activity is created again you can use that bundle to get values you stored previously and set it to appropriate view like a text field.

Enough with a single activity, How do activities communicate data between each other and other android components ?

Activities in android are not impermeable rather its porous and due to this other it can interact with other activities and system components to access data from other application using a content provider or access system services like Bluetooth.

)

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade