Lec02 - Activities in Android
Lec02 - Activities in Android
1- Activity
Dr. Lamiaa Hassaan
File Structure in Android Application
Structure of
an android
application
Android Manifest xml File
Every application must have an AndroidManifest.xml file (with
precisely that name) in its root directory.
The manifest presents essential information about the
application to the Android system, information the system must
have before it can run any of the application's code.
Android Manifest xml File
It names the Java package for the application. The
package name serves as a unique identifier for the
application.
It describes the components of the application — the
activities, services, broadcast receivers, and content
providers that the application is composed of.
It names the classes that implement each of the
components and publishes their capabilities (for example,
which intent messages they can handle). These
declarations let the Android system know what the
components are and under what conditions they can be
launched.
Android Manifest xml File
It declares which permissions the application must
have in order to access protected parts of the API
(Application Programming Interface) and interact
with other applications.
It also declares the permissions that others are
required to have in order to interact with the
application's components.
It lists the libraries that the application must be linked
with e.g., Google Maps library.
Hardware features required, e.g., camera with
autofocus.
Android Manifest xml File Example
Anatomy of Android Application
An Android application consists of four core components.
In the case of apps made of multiple parts, collaboration among the
independent core components is required for the success of the
application.
Core components are:
An Activity: A “single screen” that is visible to user
A Service: Long-running background “part” of app (not separate
process or thread)
A broadcast receiver: Component that listens for particular
Android system “events”, e.g., “found wireless device”, and
responds accordingly
A content provider: Manages app data (usually stored in
database) and data access for queries
1- Activity
A typical Android application consists of one or more
activities.
An activity is roughly equivalent to a Windows-Form .
An activity usually shows a single visual user interface
(GUI).
Only one activity (known as main) is chosen to be executed
first when the application is launched.
An activity may transfer control and data to another activity
through an interprocess communication protocol called
intents.
Example of Activities
Weather Channel App Weather Channel App Weather Channel App
GUI-1- Activity1 GUI-2- Activity2 GUI-3- Activity3
2- Service
Services are a special type of activity that do not have a visual
user interface.
Services usually run in the background for an indefinite period
of time.
Applications start their own services or connect to services
already active.
Examples:
Your background GPS service could be set to run in the background
detecting satellites, phone towers or wifi routers location information.
The service periodically broadcast location coordinates to any application
listening for that kind of data. An application may opt for binding to the
running GPS service.
3- Broadcast Receiver
A Broadcast Receiver is a dedicated listener that waits for
system-wide or locally transmitted messages.
Broadcast receivers do not display a user interface.
They typically register with the system by means of a filter
acting as a key. When the broadcasted message matches the
key the receiver is activated.
A broadcast receiver could respond by either executing a
specific activity or use the notification mechanism to request
the user„s attention.
Broadcast Receiver
4- Content Provider
A content provider is a data-centric service that makes
persistent datasets available to any number of applications.
Common global datasets include: contacts, pictures, messages,
audio files, emails.
The global datasets are usually stored in a SQLite database
(however the developer does not need to be a SQL expert)
The content provider class offers a standard set of “database-
like“ methods to enable other applications to retrieve, delete,
update, and insert data items.
4- Content Provider
A Content Provider is a wrapper that hides the actual physical data. Users
interact with their data through a common object interface
The R.java File
In Android app development, res files refer to Resource files,
which are automatically generated Java files that act as a
reference to the various resources used in an Android project and
stores information about these resources. These resources
include layouts, strings, drawables, dimensions, colors, styles,
and other similar elements. The R files play a crucial role in
accessing these resources programmatically in your code (written
in Java or Kotlin).
R.java file basically makes the connection between the XML files
and Java. The Android SDK goes over all the resources and
stores their path in the R.java file. If you attempt to make changes
directly in it, they will be erased when you build your project.
The R File
The resources in XML files (such as layouts, strings, drawables,
etc.) are all assigned a unique resource ID. When you build your
Android project, these resources and their associated resource
IDs are compiled. Based on the resources defined in XML files,
the Android Plugin generates a corresponding R.java file for
each resource type. These R.java file contains constant integer
values representing the resource IDs and are placed in the
“gen” (generated) directory of your project. You can use these
resource IDs to access the resources in your Kotlin or Java code.
How to use R.java file?
How to use it?
decide what kind of resources you want to use.
figure out what is the resource‟s name.
Finally, you type R.resourceType.resourceName.
For example, if you are trying to access a string, you type:
R.string.hello_world (assuming you have a string called
“hello_world” in your strings.xml:
<string name=”hello_world”>Hello world!</string>)
Components of A Mobile App
1- Activity
An activity is an application component that provides a
screen with which users can interact in order to do
something, such as dial a number.
Activity: key building block of Android apps
Extend Activity class
Override onCreate(), onPause(), onResume() methods
Unlike programming paradigms in which apps are launched
with a main() method, the Android system initiates code in
an Activity instance by invoking specific callback methods
that correspond to specific stages of its lifecycle.
Components of A Mobile App
1- Activity
An activity provides the window in which the app draws its
UI. This window typically fills the screen, but may be
smaller than the screen and float on top of other windows.
Generally, one activity implements one screen in an app.
To use activities in your app, you must register information
about them in the app‟s manifest, and you must manage
activity lifecycles appropriately.
Dalvik VM can stop any Activity without warning, so saving
state is important.
Activities need to be “responsive”, otherwise Android shows
user “App Not Responsive” warning.
Activity Stack
Activity Stack: Activities in the system are scheduled
using an activity stack.
When a new activity is started, it is placed on top of
the stack to become the running activity
The previous activity is pushed-down one level in the
stack, and may come back to the foreground once the
new activity finishes.
If the user presses the Back Button the current activity
is terminated and the next activity on the stack moves
up to become active.
Activity Stack
Activity Lifecycle Events
Life Cycle States: When progressing from one state to the
other, the OS notifies the application of the changes by
issuing calls to the following protected transition methods:
void onCreate(Bundle savedInstanceState)
void onStart( )
void onRestart( )
void onResume( )
void onPause( )
void onStop( )
void onDestroy( )
Activity Lifecycle Callbacks
Lifecycle Essential
States
An activity has
essentially three states:
active or running
paused
stopped
Activity Lifecycle States
It is active or running when it is in the foreground of the
screen (at the top of the activity stack).
This is the activity that has “focus” and its graphical
interface is responsive to the user‟s interactions.
Activity Lifecycle States
It is paused if it has lost focus but is still visible to the
user.
That is, another activity is on top of it and that new
activity either is transparent or doesn't cover the full
screen.
A paused activity is alive (maintaining its state
information and attachment to the window manager).
Paused activities can be killed by the system when
available memory becomes extremely low.
Activity Lifecycle States
It is stopped if it is completely obscured by another
activity.
Continues to retains all its state information.
It is no longer visible to the user ( its window is hidden
and its life cycle could be terminated at any point by the
system if the resources that it holds are needed elsewhere).
Activity
Lifecycle
Lifecycle Methods
Method: onCreate( )
Called when the activity is first created.
Most of your application‟s code is written here.
Typically used to define listener‟s behavior, initialize
data structures, wire-up UI view elements (buttons, text
boxes, lists) with local Java controls, etc.
It may receive a data Bundle object containing the
activity's previous state (if any).
Followed by onStart()
Lifecycle Methods
Method: onPause( )
Called when the system is about to
transfer control to another activity.
Gives you a chance to commit unsaved data, and stop
work that may unnecessarily burden the system.
The next activity waits until completion of this state.
Followed either by onResume() if the activity returns
back to the foreground, or by onStop() if it becomes
invisible to the user.
A paused activity could be killed by the system.
Lifetime of an Application
Complete / Visible / Foreground Lifetime
An activity begins its lifecycle when entering the
onCreate( ) state .
If not interrupted or dismissed, the activity performs its
job and finally terminates and releases its acquired
resources when reaching the onDestroy( ) event.
Coding of Lifecycle Events
Associating Lifecycle Events with Application’s Code
Applications do not need to implement each of the
transition methods, however there are mandatory and
recommended states to consider
Mandatory
All activities must implement onCreate() to do the initial
setup
when the object is first instantiated.
Highly Recommended
Activities should implement onPause() to commit data
changes in anticipation to stop interacting with the user.
Lifecycle Methods
Killable States
Activities on killable states can be terminated by the
system when memory resources become critically low.
Methods: onPause( ), onStop( ), and onDestroy( ) are
killable.
onPause( ) is the only state that is guaranteed to be
given a chance to complete before the process is killed.
You should use onPause( ) to write any pending
persistent data.