basic principles and concepts

Article by ITQ Group tester Leonid Galochkin.

Hello! My name is Leonid, I work as a tester at ITQ Group.
In this article I want to briefly and clearly describe the Activity life cycle of mobile applications on Android.

Understanding the operating principles of activity allows you to understand the logic of mobile applications, develop test cases of various levels, and prepare test documentation. The activity principles can be ignored, for example, when testing the application UI or e2e testing, but for complete MP testing it is necessary to know them.

What is the Activity Lifecycle

The Activity lifecycle is often confused with the application lifecycle in general, which is incorrect.

Activity is the main part of any Android application with which the user interacts. That is, it is the interface itself or the screen on which interaction occurs in a certain state. An Activity contains all kinds of elements: buttons, fragments, images and other View elements. View is the visual interface of the application in one state or another with which the user interacts.

A living example of the principles of activation is the operation of an application from the moment it is called to the device, going through a certain flow, closing the application and the appearance of the start of the same user flow at the moment when you reopen the application.

Activity states and callbacks

Activity can be:

  • visible to the user;

  • minimized (work in the background, but not be displayed);

  • closed or destroyed by the system.

The Activity starts when the application starts. The system gives such an application high priority. The further activity state helps to understand what to do with it, at what point to release the resources associated with it, and so on. This prioritization allows, for example, in some cases, incoming calls not to be blocked.

This is what the Activity lifecycle looks like

This is what the Activity lifecycle looks like

When an activity transitions from one state to another, the Android system makes a callback to the activity's reverse lifecycle methods, and in parallel generates a stage-specific notification after each action.
For example:

  • OnCreate(). Called when an activity is created. It contains the interface, the code of the layout itself, buttons and other functional elements of the form.

  • OnResume(). The Activity is on the screen, you can interact with it, it is in focus.

  • OnPause(). The activity application itself is in a paused state, out of focus. You cannot interact with him, but you can partially see him on the screen. For example, in the form of a dialog box, under another activity.

  • OnStop(). In this method, the activity goes into the Stopped state, that is, it is completely invisible. The onStop method should release used resources that the user does not need when he is not interacting with the activity. At the same time, all interface elements remain in the device’s memory, with which you can interact in the future. For example, if text was entered into the application field at a time when the activity was in the OnResume() status, and then the activity was transferred to the OnStop() status and back to OnResume(), the entered text will be saved.

  • onDestroy(). The activity is terminated by calling the onDestroy method, which occurs either when finish() is called, or if the system decides to kill the activity for configuration reasons (for example, screen rotation or multi-window mode).

Transition diagram of the above methods.

Transition diagram of the above methods.

I will show an example of sequential actions on a mobile device related to the creation and changes of activities. For this I use the standard Google Chrome application.

  1. There is no activity associated with this application on the first screen.

  2. Click on the Google Chrome application icon to initiate sending the onCreate() method. When the activity appears on the screen, call the onStart() method.

  3. When an activity appears on the screen and we can interact with it, we call the onResume() method.

Schemes of activity calls depending on actions in the application

For a better understanding, here is a table with diagrams of calls to activity methods depending on certain actions.

The order of calling callback methods

There is also a order in which callback methods are called. It depends on what exactly the user is doing with the application.

  • After onCreate() — onStart()

  • After onRestart() — onStart()

  • After onStart() — onResume() or onStop()

  • After onResume() — onPause()

  • After onPause() — onResume() or onStop()

  • After onStop() — onRestart() or onDestroy()

  • After onDestroy() – nothing

That's all. In the article we looked at the basic principles of activity, and I hope this will help novice specialists get acquainted and understand the logic of the application in any of the shown statuses.

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *