Introduction to Android development concepts

Document from University about Introduction to Android. The Pdf explores the graphical user interface, including visual components and layout management, and delves into Jetpack Compose for declarative UI creation and multi-threading techniques. This material is suitable for Computer science students.

See more

12 Pages

Introduction to Android
Android is composed of an Operating System and a software platform to create apps and games. It is designed
to be robust: it is based on the Linux OS Kernel. Each application runs with its own process, in its own instance
of the virtual machine. When an application is installed, a new user is created. In this way, each application
will be able to R/W only in its own part of the system. Furthermore, processes are liquid: the OS manages
resource consumption, and it can free memory if needed, by alerting processes of termination. Each
application contains a manifest file in which will be declared all the components, the permissions needed
and other configurations. Each application consists of:
Activity: it handles the user interaction and for this reason it has a GUI.
Service: it can perform long lasting tasks and runs on the background.
Content Provider: it manages application data, and it can send data to other applications that are
requesting it.
Broadcast Receiver: it waits for messages coming from the OS and can generate notifications
When the application is launched, an Intent is created and sent to the Zygote, which is a pre-warmed VM
that will perform a fork so that only the component of the application needs to be instantiated. From the
forked process the Intent is used to locate and load the APK and the manifest. This will instantiate the
application object, which is the first to be instantiated and the last to be destroyed. An intent is an abstract
description of an operation, and it consists of several parts: action to be performed, data to operate upon,
category which are further information about the component that should handle the intent. If an application
wants to handle Intent, it declares them in the manifest with filters. When an Intent is launched, OS will look
through the filters for a match.
Activities
Activity is the base class that provides a GUI from which a user can interact. The OS creates the activity and
handles its life cycle. An activity must:
Acquire the required resources
Build and configure GUI
React to events from the user interactions
Manage notifications about its life cycle
Each activity shows a single user interface, so it can handle a specific task. For this reason, an application may
contain several activities. The first one to be shown is marked in the manifest file. If an activity wants to
launch another activity it will use an Intent, which can be of the same application or another one. The OS
handles activities by creating an activity stack. The one that is shown goes on top of it, while all the previous
are shifted down of one position. When the activity is removed (e.g., back press of the user) the previous
activity goes back on top of the stack. The OS sends different notifications to track the status of an application
and the programmer must react to these, since activities can be destroyed and recreated. The lifecycle goes
through some methods:
onCreate(): called when the activity is run for the first time (and the Bundle is null) or when the
activity is launched again after being terminated (Bundle will contain status information). When the
activity stops it can save its state in a bundle. The activity exists but it is not visible.
onStart(): called when the activity becomes visible to the user. User cannot interact directly with it.
onRestart(): called when an activity that was paused is restarted again.
onResume(): called when the activity reaches the top of the stack. It is in the foreground and user
can interact with it.
onPause(): called when the activity has to be moved in second position of the stack. Here all non-
needed resources are released (e.g., persisting data is committed, listeners unregistered).
onStop(): called when the activity is no more visible to the user. Next possible notification can be
onRestart() or onDestroy().
onDestroy(): activity terminated and removed from memory.
In the onCreate() method it is needed to prepare a View and make it visible. It is in charge of presenting
content to the user and is usually made of elementary widgets connected together to form a visual tree.
Android uses the composite pattern to build the hierarchy. Among the view subclasses there are the
elementary views (which are leaves in the tree) and view containers which can internally host other views. It
is possible to create the hierarchy in three ways:
Programmatically by direct instantiation of elements. This gives flexibility because they are built
based on actual data, but also control since everything is created explicitly from the programmer.
The problem is that maintenance is difficult and does not support internationalization.
Via XML files by naming each file with a unique ID and then by inflating it from the code. Each
element of the tree will have a unique ID too, to be managed also via code. It is easy to maintain, and
the visual editor helps building the XML files. The problem is that each element needs an ID, and they
must be kept in sync between the various XML representations.
Using Jetpack Compose library, which is inspired from React and it can describe declaratively the
content and the behavior of the view. Each view is represented as a function labelled
“@Composable”. The problem is that it requires programming skills and designer find the XML files
more intuitive.
Intent is an asynchronous messaging mechanism used by the OS to associate process requests with activities.
They can be:
Implicit: the action to be performed is indicated and the OS will find the component that is able to
handle it. In this case it is provided the action, the URI (resource id) and the category.
Explicit: It ask the OS to activate a specific component within the application process.
Intents can be enriched with bundles in the form of key/value pairs. Intents can also be broadcasted and
anyone who registered a listener will receive it. Typically used to generate system wide notifications.
All the elements of an application (Activity, Service, Content Provider, Broadcast Receiver) have a common
root: the Context. It provides the functionalities to access resources (identified by unique ID in the project)
and all the application-specific classes, but also to interact with the OS.
Graphical User Interface
The usage of graphic tools facilitates in terms of operational level the design of user interfaces, but still there
are:
Cognitive issues: building a pleasing, functional and easy to learn interface is challenging. The design
of informational flow and graphic contents must be done carefully, otherwise user will get confused
and stop using the application.
Operational issues: mobile devices vary widely in terms of physical dimensions, screen resolution
and orientation, dot density. A set of possible alternative configurations should be designed by also
taking in account the different languages, cultures and metaphors and level of confidence.
General guidelines says that the first step is related to the analysis of the context and the processes to be
supported, the second step subdivide complex tasks into smaller ones, while the third step relies on general
principles of interaction design.

Unlock the full PDF for free

Sign up to get full access to the document and start transforming it with AI.

Preview

Introduction to Android

Android is composed of an Operating System and a software platform to create apps and games. It is designed to be robust: it is based on the Linux OS Kernel. Each application runs with its own process, in its own instance of the virtual machine. When an application is installed, a new user is created. In this way, each application will be able to R/W only in its own part of the system. Furthermore, processes are liquid: the OS manages resource consumption, and it can free memory if needed, by alerting processes of termination. Each application contains a manifest file in which will be declared all the components, the permissions needed and other configurations. Each application consists of:

  • Activity: it handles the user interaction and for this reason it has a GUI.
  • Service: it can perform long lasting tasks and runs on the background.
  • Content Provider: it manages application data, and it can send data to other applications that are requesting it.
  • Broadcast Receiver: it waits for messages coming from the OS and can generate notifications When the application is launched, an Intent is created and sent to the Zygote, which is a pre-warmed VM that will perform a fork so that only the component of the application needs to be instantiated. From the forked process the Intent is used to locate and load the APK and the manifest. This will instantiate the application object, which is the first to be instantiated and the last to be destroyed. An intent is an abstract description of an operation, and it consists of several parts: action to be performed, data to operate upon, category which are further information about the component that should handle the intent. If an application wants to handle Intent, it declares them in the manifest with filters. When an Intent is launched, OS will look through the filters for a match.

Activities

Activity is the base class that provides a GUI from which a user can interact. The OS creates the activity and handles its life cycle. An activity must:

  • Acquire the required resources
  • Build and configure GUI
  • React to events from the user interactions
  • Manage notifications about its life cycle Each activity shows a single user interface, so it can handle a specific task. For this reason, an application may contain several activities. The first one to be shown is marked in the manifest file. If an activity wants to launch another activity it will use an Intent, which can be of the same application or another one. The OS handles activities by creating an activity stack. The one that is shown goes on top of it, while all the previous are shifted down of one position. When the activity is removed (e.g., back press of the user) the previous activity goes back on top of the stack. The OS sends different notifications to track the status of an application and the programmer must react to these, since activities can be destroyed and recreated. The lifecycle goes through some methods:
  • onCreate(): called when the activity is run for the first time (and the Bundle is null) or when the activity is launched again after being terminated (Bundle will contain status information). When the activity stops it can save its state in a bundle. The activity exists but it is not visible.
  • onStart(): called when the activity becomes visible to the user. User cannot interact directly with it.
  • onRestart(): called when an activity that was paused is restarted again.
  • onResume(): called when the activity reaches the top of the stack. It is in the foreground and user can interact with it.. onPause(): called when the activity has to be moved in second position of the stack. Here all non- needed resources are released (e.g., persisting data is committed, listeners unregistered).
  • onStop(): called when the activity is no more visible to the user. Next possible notification can be onRestart() or onDestroy().
  • onDestroy(): activity terminated and removed from memory. In the onCreate() method it is needed to prepare a View and make it visible. It is in charge of presenting content to the user and is usually made of elementary widgets connected together to form a visual tree. Android uses the composite pattern to build the hierarchy. Among the view subclasses there are the elementary views (which are leaves in the tree) and view containers which can internally host other views. It is possible to create the hierarchy in three ways:
  • Programmatically by direct instantiation of elements. This gives flexibility because they are built based on actual data, but also control since everything is created explicitly from the programmer. The problem is that maintenance is difficult and does not support internationalization.
  • Via XML files by naming each file with a unique ID and then by inflating it from the code. Each element of the tree will have a unique ID too, to be managed also via code. It is easy to maintain, and the visual editor helps building the XML files. The problem is that each element needs an ID, and they must be kept in sync between the various XML representations.
  • Using Jetpack Compose library, which is inspired from React and it can describe declaratively the content and the behavior of the view. Each view is represented as a function labelled "@Composable". The problem is that it requires programming skills and designer find the XML files more intuitive. Intent is an asynchronous messaging mechanism used by the OS to associate process requests with activities. They can be:
  • Implicit: the action to be performed is indicated and the OS will find the component that is able to handle it. In this case it is provided the action, the URI (resource id) and the category.
  • Explicit: It ask the OS to activate a specific component within the application process. Intents can be enriched with bundles in the form of key/value pairs. Intents can also be broadcasted and anyone who registered a listener will receive it. Typically used to generate system wide notifications. All the elements of an application (Activity, Service, Content Provider, Broadcast Receiver) have a common root: the Context. It provides the functionalities to access resources (identified by unique ID in the project) and all the application-specific classes, but also to interact with the OS.

Graphical User Interface

The usage of graphic tools facilitates in terms of operational level the design of user interfaces, but still there are:

  • Cognitive issues: building a pleasing, functional and easy to learn interface is challenging. The design of informational flow and graphic contents must be done carefully, otherwise user will get confused and stop using the application.
  • Operational issues: mobile devices vary widely in terms of physical dimensions, screen resolution and orientation, dot density. A set of possible alternative configurations should be designed by also taking in account the different languages, cultures and metaphors and level of confidence. General guidelines says that the first step is related to the analysis of the context and the processes to be supported, the second step subdivide complex tasks into smaller ones, while the third step relies on general principles of interaction design.Android offers a rich family of classes derived from ViewGroup specialized in the way they manage the space assigned to them. By selecting suitable policies, it is possible to mitigate the effects of device variability. Each layout can have some properties (such as orientation, layout_width and layout_height). For width and height it is possible to specify:
  • A number plus unit of measure (es. 100dp)
  • Wrap_content: it will occupy the space needed from the total of its children
  • Match_parent: it will fill all the available space given by its parent It is also possible to add white space among widgets by specifying padding and/or margin. The possible layouts are:
  • Linear Layout: child elements arranged one next to the other and, by using the orientation property, it is possible to specify if they are arranged vertically or horizontally.
  • Relative Layout: the position of children is described in relation to each other or the parent. The alignment constraints are typically expressed using Boolean properties.
  • Frame Layout: show children superimposed (sovrapposte) on the same area. The most recent is the last to be designed and it appears above the others.
  • Scroll View: used when a view needs to display more data than what can be displayed on a single screen.
  • Constraint Layout: extension of mechanisms available in linear and relative layouts. It allows to create flat, fast and effective visual hierarchies supporting almost all needs. It is integrated with the visual editor of Android Studio and it provides:
    • Constraints: one-way relationship between two widgets. Controls how they will be positioned within the layout.
    • Chains: when a constraint is duplicated in both directions, a chain is built. This allows to share the space between views and control how much of it is given to each view. Chains can be in three modes: spread (widgets are evenly separated), spread inside (first and last touch the borders of layout) and packed (concentrated to the beginning and end of the chain). Resource management system provides automatic support to use different layouts based on the actual characteristics of a device. It is needed to add a suffix in the folder to specify layouts for different configurations. The alternative resources must be named exactly as the default one and use the same identifiers.

Android Architecture Components

Jetpack is a set of components, libraries, and tool with the aim to reduce development time, reduce boilerplate code and build robust and high-quality apps. It is divided in 4 areas:

  • Foundation: common to all apps that allows backward compatibility and unit and runtime tests, as well as benchmarks and security.
  • Architecture: classes and interfaces that help make a robust, testable, and maintainable application (e.g., lifecycles, LiveData, room, ViewModel)
  • Behavior: components that help the integration with standard Android services such as notifications, permissions, and preferences.
  • UI: components and classes that allows to improve overall UX (e.g., animations, fragments, emojis) Components of an application can be launched individually and out-of-order and can be destroyed at any time by the OS or user. For this reason, we need to perform separation of concerns. No component should store inside its own properties, application data or state information. Moreover, a component should not

Can’t find what you’re looking for?

Explore more topics in the Algor library or create your own materials with AI.