When users navigate through your application, the Activity instances transition between different states in their lifecycle and the Android system calls a series of methods on the activity.
There are some keys that you have to take into account over an Activity: creating, starting, resuming, pausing, stopping and destroying.
As you can see in the image, the Android system initiates code in an Activity instance by invoking specific callback methods that correspond to specific stages of its lifecycle.
Even if you do not need to implement all the lifecycle methods, it is important that you should implement those that ensure your application behaves the way users expect because it ensures your application behaves well in several ways.
The resumed, paused and stopped states are different from the others because these can be static: the activity can exist for an extended period of time.
The application starts when you selects the application icon and the onCreate() method of the main activity is called by the Android system. At this moment, a new instance of the Activity is created.
Also, because the onCreate() method is called once, you must perform basic application startup logic that should happen only once for the entire life of the activity.
The onStart() method is called immediately after onCreated() method and the it’s on this moment when the activity becomes visible to the user. Pausing
When something occurs and the activity change like the user navigate to another activity, the onPause() method is called by the Android system. It’s here where you have to stop the actions that you don’t want to continue while paused. Also, you should save the information that you want to recover later when the Android system come back to your Activity.
When the user come back to your application, the onResume() method is called by the Android system. It’s here where you have to get the information that you store when the activity was interrupted.
There are a few scenarios where the activity is stopped and the onStop() method is called: the user opens the Recent Apps window and switches to another application, your application starts a new activity and the user receives a phone call. It differs from pausing that the UI is no longer visible and the user is focusing in a separate activity. Here, you should release the resources that aren’t needed while the user is not using the application.
The onRestart() method is called when the Android system come back to your application. Because it’s only called when the activity resumes from the stopped state, there aren’t any guidelines for this method. Anyway, you should re-instantiate the resources you released in the onStop() method.
The onDestroy() method it’s the last callback in the activity lifecycle. The system calls it and the activity instance will be completely removed from the system memory. Even if you sometimes don’t need to implement it, you should do it if there are some resources that must be remove from the system memory.
Currently, there are a lot of researches about how to save battery energy in mobile applications, but these are the basic guidelines you have to take into account about the activity states when developing an Android application. Even if your application doesn’t use a lot of system memory, a bad practise could crash your system.
Miguel Angel Alvarez
Tags: android activity lifecycle basics
June 29th 2016