Lesson iii – Android Activity life cycle
Android Activity life cycle is a series of states through which the existence of the Activity passes. In particular, in the illustration shown, the states are represented by colored figures. The entry or exit from one of these states is notified with the invocation of a callback method by the system. The code inserted in these life cycle methods must be aligned with the purpose of the method itself so that the app can be a “good citizen” of the Android ecosystem.
For example, the first callback method that is depicted in Android Activity life cycle is
onCreate()and is precisely the onCreate of which we have overridden in the implementation of the Activity seen in the previous Lessons.
Android Activity Life cycle Methods when an app is Running
When an activity is running to interact directly with the user, three android life cycle methods are invoked:
- onCreate: the activity is created. The programmer must assign the basic configurations and define what the interface layout will be;
- onStart: the activity becomes visible. It is the moment in which functions and services can be activated that must offer information to the user;
- onResume: the activity becomes the recipient of all user input.
Android Activity Life cycle Callback Methods
Android puts the activity at rest when the user shifts his attention to another activity of the system, for example, opens a different application, receives a phone call or simply – even within the same application – is activated another Activity. This path also passes through three android life cycle callback methods:
- onPause (the inverse of onResume) notifies the ceased interaction of the user with the activity;
- onStop (counterpart of onStart) marks the end of the visibility of the activity;
- onDestroy (as opposed to onCreate) marks the destruction of the activity.
In the rest of the Android App Development Lessons, practical cases of use will be offered but for the moment we keep on a rather theoretical line. Meanwhile, consider that the callback methods are designed in pairs (a method of starting with a stop method:
onPause) and usually, the work done in the startup method – in terms of activated features and allocated resources – will be canceled in the corresponding stopping method.
The first situation that will be favorable to illustrate the invocation of the most important callback methods will be the use of Intents to move from one activity to another. It is, after all, a very common practice in Android programming.
We are sorry that this lesson was not useful for you!
Let us improve this lesson!
Tell us how we can improve this lesson?