The lifecycle of an Android application

basic programming guide android 2 The life cycle of an Android application


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

In Android there is no main method as such, but there are several methods of our business that will be called by the SSOO when important events occur. In this chapter thoroughly what these events are, and how the full cycle of an activity Android works. Official documentation offers a comprehensive explanation of this issue, here we study the most important elements along with some . common mistakes when handling them

The life cycle of Android follow this pattern:

android lifecycle The lifecycle of an Android application

events Lifecycle

onCreate (Bundle)

  • represents the time when the activity is created. This method normally generate the wizard to create a new activity on Android, and is where everything we create will need the activity. If before we saved the data of the activity in a Bundle object, we can use it to regenerate. Normally we will not use.
  • onStart ()
    • activity will happen to be on screen , but not necessarily visible . If we come to a stop, passing ago by onRestart ().
  • onRestart ()
    • Prior to onStart () when we come from a call to onStop ( ).
  • onResume ()
    • The activity will start to respond to the interaction User.
  • onPause ()
    • activity will stop responding a user interaction.
  • onStop ()
    • activity has completely passed background .
  • OnDestroy ()
    • activity will be destroyed and their resources released.
  • When we need to implement one of these methods, we adding to our activity with these profiles:

     public class extends Activity {MiActividad 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 ();}} 

    is important to keep the method call the superclass to find no surprises. The tasks of each event are above our activity should be maintained. This call will go to the beginning of input events, and at the end of the output. That way we will avoid surprises, since the elements of the activity but need not be under our control will be created before using them, and destroyed later.

    not have to add all Events , which need not use the default implementation. The methods often use-and it is not recommended to touch the others are onCreate, onPause, and onRestart

    onCreate The meaning is clear.’s Where we need to load the resources, views, and anything else we need. For output, the only way in which we will focus is onPause. The reason to avoid onStop and OnDestroy is that we have no control over them. onPause will always run the application out of the foreground, while the other two run the operating system according to their needs. may never get to run! This is to avoid the cost of having to create activity again and again if we pass from the activity and the desktop, and the operating system will only release the resources used if needed, and not have to attend all proceedings.

    That implies that we assume that the application onPause die after running, and it is our last chance to save data we need to save, and to stop using services that we are, as geolocation. If services have stopped, the right place is to restart onRestart.

    Other methods do not need to use much. A typical case in which itself will be when we need integrate third-party libraries, such as Facebook or Flurry. In these cases, we ask you to join our business methods to your code. For example, to register a Flurry session we were asked to start the session in the onStart method.

    Some useful ideas

    • Remember what each event . Always need onCreate, and quite often onPause and onResume need to stop and restart the services most consumed.
    • not touch other events if not explicitly need.
    • not trust onStop and OnDestroy could never called . Save everything you need in onPause.
    • Avoid using static non-final variables . The application can still loaded when you return, and retain the values ​​that were left. . If you have no choice but to use them, be sure to reset the values ​​back to the foreground

    More info – Basic Android Programming Guide

    Article The lifecycle of an Android application was originally published on Androidsis .






    Androidsis

    The lifecycle of an Android application
    Source: english  
    December 11, 2013


    Next Random post