The life cycle of an Android application

basic-guide-programming-android-2

When you start programming in a language like C ++ or Java, the first thing that is taught is the main method, the point that the operating system will call when we start our application.

In Android there is no main method as such, but there are several methods of our activity that will be called by SSOO when they occur important events. In this chapter we will study in depth what those events are, and how it works. the complete cycle of an activity of Android. The official documentation offers an extensive explanation of this topic, here we will study the most important elements along with some common mistakes when handling them.

The life cycle of Android follows this scheme:

android-lifecycle

Life cycle events

  1. onCreate (Bundle)
    • Represents the moment when the activity is created. This method will normally be generated by the wizard when creating a new activity on Android, and it is where we will create everything the activity will need. If we have previously saved the activity data in a Bundle object, we can use it to regenerate it. Normally we will not use it.
  2. onStart ()
    • The activity will go on to be on screen, although not necessarily visible. If we come from a stop, we will go through onRestart () first.
  3. onRestart ()
    • Previous to onStart () when we come from a call to onStop ().
  4. onResume ()
    • The activity will start respond to interaction of user.
  5. onPause ()
    • The activity will stop responding to user interaction.
  6. onStop ()
    • The activity has gone completely to background.
  7. onDestroy ()
    • Activity it will be destroyed and your resources released.

When we need to implement one of these methods, we will do it adding to our activity with these profiles:

public class MyActivity extends Activity {protected void onCreate (Bundle savedInstanceState) {super.onCreate (savedInstanceState); ...} protected void onStart () {super.onStart (); ...} protected void onRestart () {super.onRestart (); ...} protected void onResume () {super.onResume (); ...} protected void onPause () {... super.onPause (); } protected void onStop () {... onStop (); } protected void onDestroy () {... super.onDestroy (); }}

It is important to maintain superclass method call so as not to encounter surprises. The tasks of each event that are above our activity must be maintained. This call will go to the beginning of the input events, and to the end of the output events. In this way we will avoid surprises, since the elements of the activity that we need but that will not be under our control will be created before using them, and will be destroyed afterwards.

We don't have to add all the events, the ones we don't need will use the default implementation. The methods that we will often use - and it is not recommended to touch the others - are onCreate, onPause, and onRestart.

The meaning of onCreate is clear: it is the place where we will load the resources we need, the views, and whatever else we need. For the output, the only method we will focus on is onPause. The reason for avoiding onStop and onDestroy is that we don't have any control over them. onPause will run whenever the application comes out of the foreground, while the other two will be run by the operating system based on your needs. They may never be executed! This is done to avoid the cost of creating the activity again and again if we go from the activity to the desktop and vice versa, and the operating system will only free the resources used if it needs them, and it does not have to attend all the open processes.

That implies that we will assume that the application will die after executing onPause, and that it is ours last chance to save data that we need to save, and to stop services that we are using, such as geolocation. If we have stopped services, the appropriate place to restart them is onRestart.

The other methods we will not need to use much. A common case in which we will need it will be when we integrate third-party libraries, such as Facebook or Flurry. In these cases, we will be asked to match the methods of our activity to your code. For example, to register a Flurry session we will be asked to start the session in the onStart method.

Some useful ideas

  • Remember what each event is for. You will always need onCreate, and quite often you will need onPause and onResume to stop and relaunch the most consuming services.
  • Don't touch the other events if you don't need it expressly.
  • Don't trust onStop and onDestroy, they might never be called. Save everything you need in onPause.
  • Avoid using non-final static variables. The app can still be loaded when you return, and they will retain the values ​​they left behind. If you have no choice but to use them, be sure to reset their values ​​when you return to the foreground.

More information - Basic guide to programming in Android


You are interested in:
How to remove viruses on Android
Follow us on Google News

Leave a Comment

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

*

*

  1. Responsible for the data: Actualidad Blog
  2. Purpose of the data: Control SPAM, comment management.
  3. Legitimation: Your consent
  4. Communication of the data: The data will not be communicated to third parties except by legal obligation.
  5. Data storage: Database hosted by Occentus Networks (EU)
  6. Rights: At any time you can limit, recover and delete your information.