Android. Second. Activities life cycle

1.1. Presentation

This exercise is an introduction to Android application development.

1.2. Competencies

The following skills will be worked on:

  • Ability to program applications for Android devices.
  • Ability to integrate Google APIs and services, such as the map service.

1.3. Goals

The objectives are for the student to learn to:

  • Know the life cycle of an application's activities.
  • Create and code the activities of an application.
  • Send data between activities of an application.
  • Create and code XML files that define the graphical interface of the application.
  • Integrate the map services and functionalities offered by Google through the Google Maps API v2.
  • Use GPS to position the mobile device.
  • Use data persistence through SharedPreferences.

1.4 Exercises

GitHub

The objective of this exercise is to understand the life cycle of the activities. A project has been created on GitHub (collaborative software development platform to store projects using the control system of Git versions) that you can download by following the following steps:

  • Open Android Studio.
  • Go to tab
  • VCS > Checkout from version control > GitHub

    Ejemplo importar proyecto GitHub con Android Studio
    1.Example import GitHub project with Android Studio.

  • Or if you do not have any project open, directly from the Quick Start choose the option
  • Checkout from version control > GitHub

    Ejemplo importar proyecto GitHub con Android Studio.
    2. Example import GitHub project with Android Studio.

  • Once selected, in the Vcs Repository URL option write the following URL and choose the destination location.

Once the project is open, run it on the emulator/mobile device and answer the following questions:

    Explains what the different states of an activity are and when each of these states occurs (see section 2.1 of the wiki).

    First of all, in my case I had to install Git to be able to export the project. To perform this task install the following link the git.exe program. Once this is finished installation I add the GIT application path as shown in the figure [3] .

    Ruta Git
    3. Git path

    If we apply the test, we can see that, if we have carried out the installation correctly, the message appears. the figure [4]

    Exito instalación Git
    4. Git installation success

    Once this problem has been solved, I download the application and we can explain what the status of a Activity.

    First of all, to analyze the states that an Activity has, we must know its definition. one Android application will be made up of a set of basic elements of interaction with the user, known as activities. Additionally, the activities are composed of the following methods:

    • onCreate(): Called when the activity is created. It is used to perform all types of initializations, such as creating the user interface or initializing data structures.
    • onResume(): Called when the activity is going to start interacting with the user. It is a good place for launch the animations and music.
    • onStart(): called when the activity is visible to the user. Indicates that the activity is going to be represented after having passed through onStop().
    • onPause(): Called when the activity is paused. Then you can call again. It is the right place to stop animations, music or store data that was being edited.
    • onStop(): called when the activity is no longer visible to the user. Be careful if there is very little memory, it is The activity may be destroyed without calling this method.
    • onDestroy(): Called just before the activity is destroyed by the system. For example, when the user presses the back button or when the finish() method is called. Be careful if there is very little memory, it is possible cause the activity to be destroyed without calling this method.
    • onRestart(): called when the activity has stopped and is restarted.

    The different types of states of an Activity are the following:

    • Active (Running): The activity is on top of the stack, which means it is visible and has focus.
    • Visible (Paused): The activity is visible but does not have focus. This state is reached when it becomes active another activity with some transparent part or that does not occupy the entire screen. When an activity is hidden completely, it becomes stopped.
    • Stopped: When the activity is not visible. The programmer must save the interface state user, preferences, etc.
    • Destroyed: When the activity ends when the finish() method is invoked, or is killed by the system.

    Every time an activity changes state, events will be generated that can be captured by certain methods. of the activity. Below is a schematic illustrating the methods that capture these events.

    Ciclo de vida de una actividad
    5. Life cycle of an activity

    Using Logs, identify which states each activity passes through in the following cases:
    • When the app is opened for the first time.
      • onCreate
      • onStart
      • onResume

      Procesos mostrados por el log
      6. Processes shown by the log

    • When we go from activity A to B.
      • onPause ActivityA
      • onCreate ActivityB
      • onStart ActivityB
      • onResume ActivityB
      • onStop ActivityA

      Procesos mostrados por el log
      7.Processes shown by the log

    • When we go back using the back button (B \rightarrow A).
      • onPause ActivityB
      • onRestart ActivityA
      • onStart ActivityA
      • onResume ActivityA
      • onStop ActivityB
      • onDestroy ActivityB

      Procesos mostrados por el log
      8. Processes shown by the log

    • When the dialog is opened while in any activity and then we close it.
      • onPause ActivityA
      • onResume ActivityA

      Procesos mostrados por el log
      9. Processes shown by the log

    What would we have to modify to make activity C the one that is executed for the first time when We open the application.

    The change that appears in the figure [10] marked in the green circle. We indicate in the file Manifest.xml that Activity C will be the first to be launched and Activity A becomes only an activity more.

    Captura del documento Manifest.xml
    10. Capture of the Manifest.xml document

    What are intents for? What types are there? What type is used in this exercise to change screens?

    Intents offer a message passing service that allows interconnecting components of the same or different applications. Typically used to invoke a new activity or to send events to multiple activities (called broadcast intents). Intents are managed by event or broadcast receivers receivers, which can listen to any intent.

    Its attributes are the following:

    • The name of the component we want to report. For example, the email application.
    • The action that you want to launch. For example, edit, call, sync, low battery information, etc.
    • The data about the action. For example, writing a new email.
    • The extra information. The recipient's email address and title.
    There are two types of intentions:
    • Explicit intentions: the exact component to be launched is indicated. Its typical use is to go running the different internal components of an application.
    • Implicit intentions: It is the platform that determines, through the intention resolution process, which component is the most appropriate to handle
    • the attempt
    Regarding how intents are used, we can define the following cases
    • Launch an activity (startActivity() and startActivityForResult())
    • Start a service (startService())
    • Launch a broadcast announcement (sendBroadcast())
    • Connect to a service (bindService())
    In our program we use the intent to launch an Activity.

Bibliografía