Unit 3
Unit 3
UNIT III
1. If an activity is in the foreground of the screen i.e at the top of the stack, then it is said to
be active or running. This is usually the activity that the user is currently interacting with.
2. If an activity has lost focus and a non-full-sized or transparent activity has focused on top
of your activity. In such a case either another activity has a higher position in multi-
window mode or the activity itself is not focusable in the current window mode. Such
activity is completely alive.
3. If an activity is completely hidden by another activity, it is stopped or hidden. It still
retains all the information, and as its window is hidden thus it will often be killed by the
system when memory is needed elsewhere.
4. The system can destroy the activity from memory by either asking it to finish or simply
killing its process. When it is displayed again to the user, it must be completely restarted
and restored to its previous state.
For each stage, android provides us with a set of 7 methods that have their own significance for
each stage in the life cycle.
1. onCreate():
It is called when the activity is first created. This is where all the static work is done like
creating views, binding data to lists, etc. This method also provides a Bundle containing its
previous frozen state, if there was one.
2. onStart()
It is invoked when the activity is visible to the user. It is followed by onResume() if the
activity is invoked from the background. It is also invoked after onCreate() when the activity
is first started.
3. onRestart()
It is invoked after the activity has been stopped and prior to its starting stage and thus is
always followed by onStart() when any activity is revived from background to on-screen.
4. onResume()
It is invoked when the activity starts interacting with the user. At this point, the activity is at
the top of the activity stack, with a user interacting with it. Always followed by onPause()
when the activity goes into the background or is closed by the user.
5. onPause()
It is invoked when an activity is going into the background but has not yet been killed. It is a
counterpart to onResume(). When an activity is launched in front of another activity, this
callback will be invoked on the top activity (currently on screen). The activity, under the
active activity, will not be created until the active activity’s onPause() returns, so it is
recommended that heavy processing should not be done in this part.
6. onStop()
It is invoked when the activity is not visible to the user. It is followed by onRestart() when
the activity is revoked from the background, followed by onDestroy() when the activity is
closed or finished, and nothing when the activity remains on the background only. Note that
this method may never be called, in low memory situations where the system does not have
enough memory to keep the activity’s process running after its onPause() method is called.
7. onDestroy()
The final call received before the activity is destroyed. This can happen either because the
activity is finishing (when finish() is invoked) or because the system is temporarily
destroying this instance of the activity to save space. To distinguish between these scenarios,
check it with isFinishing() method.
Permissions on Android
App permissions help support user privacy by protecting access to the following:
Types of permissions
1) Install-time permissions
Install-time permissions give your app limited access to restricted data or let your app perform
restricted actions that minimally affect the system or other apps. When you declare install-time
permissions in your app, an app store presents an install-time permission notice to the user
when they view an app's details page. he system automatically grants your app the permissions
when the user installs your app. Android includes several sub-types of install-time permissions,
including normal permissions and signature permissions.
a) Normal permissions
These permissions allow access to data and actions that extend beyond your app's sandbox but
present very little risk to the user's privacy and the operation of other apps.
b) Signature permissions
The system grants a signature permission to an app only when the app is signed by the same
certificate as the app or the OS that defines the permission.
Applications that implement privileged services, such as autofill or VPN services, also make
use of signature permissions. These apps require service-binding signature permissions so that
only the system can bind to the services.
2) Runtime permissions
Runtime permissions, also known as dangerous permissions, give your app additional access to
restricted data or let your app perform restricted actions that more substantially affect the
system and other apps. Therefore, you need to request runtime permissions in your app before
you can access the restricted data or perform restricted actions. Don't assume that these
permissions have been previously granted—check them and, if needed, request them before
each access.
Many runtime permissions access private user data, a special type of restricted data that
includes potentially sensitive information. Examples of private user data include location and
contact information. The microphone and camera provide access to particularly sensitive
information.
3) Special permissions
Special permissions correspond to particular app operations. Only the platform and OEMs can
define special permissions. Additionally, the platform and OEMs usually define special
permissions when they want to protect access to particularly powerful actions, such as drawing
over other apps.
The Special app access page in system settings contains a set of user-toggleable operations.
Many of these operations are implemented as special permissions.
Basic UI Components
In android UI or input controls are the interactive or View components that are used to design
the user interface of an application. In android we have a wide variety of UI or input controls
available, those are TextView, EditText, Buttons, Checkbox, Progressbar, etc.
We have a different type of UI controls available in android to implement the user interface for
our android applications. Following are the commonly used UI or input controls in android
applications.
TextView: In android, TextView is a user interface control that is used to display the
text to the user.
EditText: In android, EditText is a user interface control which is used to allow the
user to enter or modify the text.
Button: In android, Button is a user interface control that is used to perform an action
when the user clicks or tap on it.
RadioButton: In android, Radio Button is a two-states button that can be either
checked or unchecked and it cannot be unchecked once it is checked.
ProgressBar: In android, ProgressBar is a user interface control which is used to
indicate the progress of an operation.
CheckBox: In android, Checkbox is a two-states button that can be either checked or
unchecked.
ImageButton: In android, Image Button is a user interface control that is used to
display a button with an image to perform an action when the user clicks or tap on it.
Generally, the Image button in android looks similar as regular Button and perform the
actions same as regular button but only difference is for image button we will add an
image instead of text.
TimePicker: In android, TimePicker is a widget for selecting the time of day, either in
24-hour or AM/PM mode.
DatePicker: In android, DatePicker is a widget for selecting a date.
ToggleButton: In android, Toggle Button is a user interface control that is used to
display ON (Checked) or OFF (Unchecked) states as a button with a light indicator.
Event Listeners
Event Handlers
Event Listener Registration
Event Listeners
Event Listener is an interface present in the View Class. You have a single callback method for
each event. This method gets triggered when the view bound with an event comes into the
user’s interaction.
Below are some of the essential event listeners:
Event Handlers
These are the methods that have the specified action that is required to be performed. Usually,
these methods are present in the event listener call-back method. It is used to define the task
that has to be completed. Event listeners, when called trigger event handlers.
Some of the commonly used event handlers are as follows:
Event Listener Registration is done in three ways that are described as follows:
1. First, you can directly register the event listeners using the activity’s XML file. For example,
in buttons, we can now provide the onClick attribute to register the event listener.
2. You can register the event listener by writing it in an Activity class that extends properties
from the Listener Class.
3. The last method is by dynamically setting the listener on the element.
Touch Mode
Usually, you can find many ways to interact with your application. It may be through buttons,
screen, gestures, hardware or even volume buttons. By Touch mode, we mean to give access to
the application to respond to the touch events.
One pervasive example is your screenshots. Whenever you wish to take a screenshot, you use
your three fingers to drag on the screen. Another ubiquitous example is your image zooming.
You can double-tap or use your two fingers to zoom in on the image.
To check whether your application responds to touch events or not, you can call
the isInTouchMode().
Focus Mode
Focus is a basic mode for any application. This mode shows which element is currently in
focus. It helps the user to take care of the interactions he makes.
One typical example is when the user inputs data in the EditText element, then it gets
highlighted.
To check whether an element is focusable or not, you can call the isFocusable() method. You
can even alter the element focus by changing the value in the setFocusable() method. You can
call the isFocusableInTouchMode() to know whether or not the element is focusable in touch
mode.
Android Layouts
Android Layout is used to establish the user interface that contains the UI controls and widgets
that will be displayed on the screen of an Android application or activity screen. Every
application is often a combination of View and ViewGroup. As is common knowledge, an
Android application has a huge number of activities, each of which can be considered a page of
the program. Hence, each activity comprises numerous user interface components, which are
instances of View and ViewGroup. All of a layout’s elements are constructed utilizing a
hierarchy of View and ViewGroup objects.
View: A View is the user interface used to construct interactive UI components such
as TextView, ImageView, EditText, RadioButton, etc., and is responsible for processing events
and rendering. They are commonly referred to as widgets.
View Group: A ViewGroup serves as the basic class for layouts and layout parameters that
contain additional Views or ViewGroups and define layout characteristics. They are typically
referred to as layouts.
1) Linear Layout- This view group aligns all of its children in a single direction, either
vertically or horizontally. A vertical layout has a column of Views, whereas in a horizontal
layout there is a row of Views. It supports a weight attribute for each child View that can
control the relative size of each child View within the available space. The
android:orientation attribute specifies the layout’s direction.
2) Relative Layout: It allows Views to be positioned in relation to each other or to the parent
container. It offers greater flexibility than Linear Layout as it enables developers to create
complex layouts without the need for nested containers. For example, you can align a button
to the right of a TextView or at the center of the screen. It’s ideal for designing interfaces
where the position of elements depends on others.
3) Frame Layout: Frame Layout is designed to block out an area on the screen to display a
single item. Generally, Frame Layout should be used to hold a single child view, because it
can be difficult to organize child views in a way that’s scalable to different screen sizes
without the children overlapping each other. You can, however, add multiple children to a
Frame Layout and control their position within the Frame Layout by assigning gravity to
each child, using the android:layout_gravity attribute. Child views are drawn in a stack, with
the most recently added child on top.
4) Absolute Layout: It enables you to specify the exact location of child Views. Each View
can be positioned with specific x and y coordinates. However, it’s not recommended for use
as it doesn’t adapt well to different screen sizes and orientations. Due to its lack of
flexibility and the complexity of maintaining it across various screen sizes, it’s generally
avoided in modern Android development.
5) Table Layout: As the name suggests, it arranges Views in a grid-like format, similar to a
table. It’s useful for displaying rows of data or elements that need to be aligned in columns
and rows. Each row in a Table Layout can contain multiple Views, and each View is placed
in a cell. This layout is beneficial when creating calculators, timetables, or any interface
with a tabular structure.
Layout Attributes
Following are common attributes and will be applied to all the layouts:
Attribute Description
android:id This is the ID which uniquely identifies the view.
android:layout_width This is the width of the layout.
android:layout_height This is the height of the layout
android:layout_marginTop This is the extra space on the top side of the layout.
android:layout_marginBottom This is the extra space on the bottom side of the layout.
android:layout_marginLeft This is the extra space on the left side of the layout.
android:layout_marginRight This is the extra space on the right side of the layout.
android:layout_gravity This specifies how child Views are positioned.
android:layout_weight This specifies how much of the extra space in the
layout should be allocated to the View.
android:layout_x This specifies the x-coordinate of the layout.
android:layout_y This specifies the y-coordinate of the layout.
android:layout_width This is the width of the layout.
android:paddingLeft This is the left padding filled for the layout.
android:paddingRight This is the right padding filled for the layout.
android:paddingTop This is the top padding filled for the layout.
android:paddingBottom This is the bottom padding filled for the layout.
Intent in Android
In Android, it is quite usual for users to witness a jump from one application to another as a
part of the whole process, for example, searching for a location on the browser and
witnessing a direct jump into Google Maps or receiving payment links in Messages
Application (SMS) and on clicking jumping to PayPal or GPay (Google Pay). This process
of taking users from one application to another is achieved by passing the Intent to the
system. Intents, in general, are used for navigating among various activities within the same
application, but note, is not limited to one single application, i.e., they can be utilized from
moving from one application to another as well.
Below are some applications of Intents:
1. Sending the User to Another App
2. Getting a Result from an Activity
3. Allowing Other Apps to Start Your Activity
1) Implicit Intent
In Implicit Intents we do need to specify the name of the component. We just specify
the Action which has to be performed and further this action is handled by the
component of another application.
The basic example of implicit Intent is to open any web page
2) Explicit Intent
Explicit Intents are used to connect the application internally.
In Explicit we use the name of component which will be affected by Intent. For
Example: If we know class name then we can navigate the app from One Activity to
another activity using Intent. In the similar way we can start a service to download a file
in background process. Explicit Intent work internally within an application to perform
navigation and data transfer.
Intent Uses In Android:
Android uses Intents for facilitating communication between its components like Activities,
Services and Broadcast Receivers.
Whenever you need to navigate to another activity of your app or you need to send some
information to next activity then we can always prefer to Intents for doing so.
Intents are really easy to handle and it facilitates communication of components and activities
of your application. Moreover you can communicate to another application and send some data
to another application using Intents.