What are Android Activities and what is their lifecycle? - Technology
Pular para o conteúdo

What are Android activities and what is their lifecycle?

One of the main concepts in the development of mobile applications is the concept of Activity, these being the basis of the application, this time we will see what they consist of and what is their life cycle.

Adverts


On the official Android developer page we can find a more detailed explanation of what the activities represent (https://developer.android.com/guide/components/activities/intro-activities) here we will see a generality about them and their life cycle.


We have already seen that an Android application is made up of different graphical components or views and, in addition, they have a directory structure that allows the graphical interface to be independent of the application's logic, the screens are managed through the xml files that contain the layouts or content managers and the logic is worked in Activities that correspond to the Activity class, there is also another concept called fragments that live inside activities, but this is something we will cover later.


The Activity class is one of the main components of an application. They are precisely the basis for building apps, as they manage the system logic and what the user will interact with. An android application starts with an instance of an Activity that has a defined lifecycle to keep the application running.


Before diving into the lifecycle it is very important to make it clear that how a mobile app works differs from how other apps work, a mobile app does not have a specific starting point, unlike a desktop app which starts through a main() method, an application can be started from any of its screens (this doesn't mean we can't give a start order).


Internally, when we change screens, what is done is a call between activities (or the fragments are replaced in the same activity) and when we change between applications in the same way, the application is invoked by calling its initial activity.


An application is usually composed of several screens, which is equivalent to having several activities that communicate with each other, although it has already been mentioned that the application can be started from any activity, an activity is always defined as the main activity MainActivity (although not must necessarily be the first to be displayed) and from there mark the starting point for navigation in the system calling new activities or fragments.

Manifest file (AndroidManifest.xml)


In a previous entry we talked about the manifest file, this is a configuration file that allows you to parameterize the main elements of the application, here you will find information such as the application ID, the name, the icon, the activities it contains, the permissions , among other important items.



When an activity is created, androidStudio automatically takes care of parameterizing it in the AndroidManifest.xml file and there you can also add filters or properties to determine additional actions of the application, for example in the previous image you can see that the MainActivity is the activity system main (the one shown first) since the is added to it with the property <action android:name="android.intent.action.MAIN" />

Activity lifecycle.


An Activity goes through several states during its execution, these states occur through user interaction with the Activity, for example when it enters, exits, enters again or when it is closed, android allows us to control each of these states through a series of defined methods that we can implement to manage the behavior, for example, you can pause a video player when changing the screen or cancel connection processes when exiting the application, on the Android developer page we find the detail of these methods (there we can Also find the following diagram that shows how it works:


Here's a summary of each of the states:


  • onCreate()

    This method is created by default in the activity and is activated when it starts to reference the layout corresponding to the graphic part through the setContentView() method, this is the first method that is executed.

  • onStart()

    When onCreate() closes, the activity changes its state from created to started and is presented to the user, this is where the view and activity become interactive.

  • In short()

    After starting the activity, the onResume() state is changed, which is responsible for processing user interaction information, here everything the user enters is recognized and captured.

  • onPause()

    In case the Activity loses focus and is interrupted, the Pause state is called, for example, when the user presses the back button or changes screen, it means that the activity is still partially visible while the activity is being closed.

    From this state you can return again to onResume() or onStop()

  • onStop()

    This state starts when the activity is no longer visible to the user, it can be because the activity was deleted, one that was stopped is being reactivated or because a new one is starting, in this case the stopped activity is no longer visible to the user. the user, from here you can call onRestart() or onDestroy()

  • onRestart()

    The system enters this state when a stopped activity is going to be started again, here the activity state is restored from the moment it was stopped and the call to onStart() is made.

  • onDestroy()

    Finally, the system calls this method before deleting the activity, this is the last state the activity goes through and is implemented when closing or releasing all resources of an activity must be guaranteed.


These methods can be implemented in the project as follows:


So when starting the application we will see the behavior in logcat.

And that's it, with that we see what the activities consist of and how we can control the lifecycle, then we can continue working on the basic application.

It may also interest you.




Is there anything you want to add or comment about this entry? feel free to do….And if you liked it... I invite you to share Y Subscribe by clicking on the “Join this site” button to hear more posts like this one 😉