Android Classes inheriting from the Activity superclass

Android Classes inheriting from the Activity superclass

Intents and Android context In Android systems, applications run in compartmentalized environments so that they do not impact the behavior of other executions. Intents are part of the exchange mechanism between application resources to inform the system of an intention to access a resource. The Intent class describes an action or an event including a context, a component, extras and option flags. The intent also enables sending a Bundle (in which parameters or objects are packaged by key– value pairs) between activity instances and their derivatives, for example a background service (i.e. daemon process) or a Broadcast Receiver listening to the messages sent through the system and to which it subscribed. The Context object of Android makes it possible to access the application’s information and status, but it also enables access to resources available in the application context, as the application is a context in itself, which implements the Context interface of Android.

The Activity class of Android Classes inheriting from the Activity superclass are Android classes intended for the management of user interactions. An Activity allows the management of the graphical interface (screen): an activity is usually linked to a view (described in XML in layout files found in the res folder) for the management of user’s actions on menu options, buttons, keyboard entries, touch events on the screen or even the voice (Google Talk). NOTE.– An Android application with a user interface (screen) is thus made up of at least one activity; in this case, we can assume that this is the main activity (action filter “MAIN”) automatically launched when the user taps on the application icon.

Android activity Lifecycle One of the assets of Android development is to release the developer from managing memory resources, especially through the implementation of the Activity class. The lifecycle pattern is showing the methods inherited from the superclass that are called at each state of the lifecycle of an activity: these methods can be overridden (@override note) to execute the code you implement for this state transition. NOTE.– When an activity is no longer in the foreground (the user launches another activity), its state (on pause or on stop) can vary according to the flags passed as parameters when the activity was started (see section This has to be taken into account in the way of coding initializations in callback14 methods of the activity’s lifecycle so not to launch the same activity several times when one (or several) is already on pause. In the same way, when an activity is started (or resumed), it does not necessarily go through onCreate or onStart callbacks, but it always goes through the onResume method: be careful to properly place initialization and objects release code in the right method.

Management of an activity

As illustrated by the code the start activity method launches and puts an activity on the foreground, it takes an intent as parameter and also an activity class to activate.

NOTE.– Depending on the intent and on the parameters passed, the startActivity method inherited from the Android context (see section 2.1.3) is also used to launch actions such as e-mail sending, URL display in the browser. When an activity is called with an expected result in return, using the startActivityForResult method will callback the onActivityResult method of the caller activity. Then, the origin of the request can be identified by a code returned by the activity called joint to a data intent (null value if useless). You just need to override the callback method in order to check the origin of the request and the returned result to carry out the proper processing. For example, the code shows how to convert a text into a vocal message and how to start a voice recognition activity with TextToSpeech, then collect the text in an array of strings with the onActivityResult method.

In order to finish the activity that, for example, was triggered by an action from the user, we just need to call the method finish(). In that case, if another activity is in the top of the stack of calls of the application, it will replace the finished activity on the foreground.