Android
Module 3
For Notes download BCA Resources App
Android Lifecycle
• The Android Activity Lifecycle defines how an activity transitions through
different states from its creation to its destruction.
• The lifecycle of an activity consists of several stages, each corresponding to a
specific method in the activity class. These methods allow developers to
perform actions when the activity enters or leaves a particular state.
1. onCreate(Bundle savedInstanceState)
2. onStart()
3. onResume()
4. onPause()
5. onStop()
6. onRestart()
7. onDestroy()
For Notes download BCA Resources App
Android Lifecycle
onCreate(Bundle savedInstanceState)
• The onCreate() method is the first method called when an activity is
created.
• It is used to perform initial setup tasks, such as setting up the user
interface, initializing variables, and restoring previously saved state if the
activity is being recreated.
• Typically, developers use this method to call setContentView() to define
the activity’s layout using an XML file.
• This method is called only once in the activity’s lifecycle, unless the
activity is destroyed and recreated, such as during a screen rotation.
For Notes download BCA Resources App
Android Lifecycle
For Notes download BCA Resources App
Android Lifecycle
onStart()
• After onCreate() is executed, the activity enters the onStart() state. This
method indicates that the activity is becoming visible to the user but is
not yet ready for interaction.
• It is a good place to initialize components that are visible to the user,
such as animations. However, intensive tasks should not be performed
here to avoid delaying the UI.
For Notes download BCA Resources App
Android Lifecycle
For Notes download BCA Resources App
Android Lifecycle
onResume()
• The onResume() method is called when the activity enters the foreground and
becomes interactive.
• This state is where the activity remains until a user action or system event
causes it to lose focus.
• It is the ideal place to start animations, acquire exclusive resources, or register
broadcast receivers.
• Activities remain in this state until they are paused or stopped.
For Notes download BCA Resources App
Android Lifecycle
For Notes download BCA Resources App
Android Lifecycle
onPause()
• When the activity loses focus but is still partially visible, the onPause() method
is called. This state can occur when a new activity starts or a dialog appears.
• It is used to pause ongoing tasks such as animations, video playback, or
resource-intensive operations. Developers should also save any data that
needs to persist if the activity is stopped.
• However, only lightweight operations should be performed here, as the
system can kill the process if it takes too long.
• After onPause(), the activity may either resume if the interruption ends or
proceed to onStop().
For Notes download BCA Resources App
Android Lifecycle
For Notes download BCA Resources App
Android Lifecycle
onStop()
• When the activity is no longer visible to the user, onStop() is called. This
state occurs when a new activity covers the entire screen.
• It is essential to release resources, stop background tasks, and save
persistent data here.
• For example, ongoing network requests or database operations should
be paused to prevent battery drain and memory leaks.
• After onStop(), the activity can be restarted with onRestart() or
destroyed with onDestroy().
For Notes download BCA Resources App
Android Lifecycle
For Notes download BCA Resources App
Android Lifecycle
onRestart()
• The onRestart() method is called if the activity was previously stopped
and is now being restarted.
• It acts as a bridge between onStop() and onStart(), enabling developers
to reinitialize resources that were released in onStop().
• It is useful for refreshing UI components if needed.
For Notes download BCA Resources App
Android Lifecycle
For Notes download BCA Resources App
Android Lifecycle
onDestroy()
• The onDestroy() method is called before an activity is completely
destroyed and removed from memory.
• This method is used for final cleanup, such as releasing resources and
saving persistent data.
• It can be triggered by the user explicitly closing the app, the system
freeing memory, or a configuration change like a screen rotation.
• It is important to note that onDestroy() is not guaranteed to be called,
especially if the system forcibly kills the process.
For Notes download BCA Resources App
Android Lifecycle
For Notes download BCA Resources App
Broadcast Lifecycle
• The Broadcast Lifecycle in Android is an essential part of the Android
application framework that enables applications to respond to system-
wide and app-specific events.
• Broadcasts are messages sent by the Android system or applications to
notify other components about events such as battery changes, network
connectivity updates, incoming calls, and custom events triggered by
apps.
• The broadcast mechanism in Android follows a structured lifecycle to
ensure efficient and timely delivery of these messages.
For Notes download BCA Resources App
Broadcast Lifecycle
There are following two important steps to make Broadcast Receiver
works for the system broadcasted intents/event:
• Creating the Broadcast Receiver
• Registering Broadcast Receiver
For Notes download BCA Resources App
Creating the Broadcast Receiver
A broadcast receiver is implemented as a subclass of BroadcastReceiver class
and overriding the onReceive() method where each message is received as a
Intent object parameter.
For Notes download BCA Resources App
Registering Broadcast Receiver
• An application listens for specific broadcast intents by
registering a broadcast receiver in [Link]
file.
• Consider we are going to register MyReceiver for system
generated event ACTION_BOOT_COMPLETED which is
fired by the system once the Android system has
completed the boot process.
• Now whenever your Android device gets booted, it will
be intercepted by BroadcastReceiver MyReceiver and
implemented logic inside onReceive() will be executed.
For Notes download BCA Resources App
Services
• Service is a component that runs in the background to perform long-
running operations without a user interface.
• Unlike activities, services are designed to handle tasks even when the
application is not in the foreground.
• Services are particularly useful for operations such as playing music,
handling network transactions, downloading files, and processing data.
• A service can be started by an application component, and it continues
to run even if the user switches to another app.
• Once started, a service may run indefinitely unless explicitly stopped or
terminated by the system to reclaim resources.
For Notes download BCA Resources App
Types of Services
1. Foreground Service: A foreground service performs an operation noticeable to the user, such as
playing music or tracking location. It shows a persistent notification in the status bar to ensure
users are aware of its ongoing operation.
Example: Music player, GPS navigation.
2. Background Service: A background service performs tasks that are not directly visible to the user.
Example: Syncing data, processing files.
3. Bound Service: A bound service allows components (such as activities) to bind to it and interact
through a client-server interface. The service runs as long as components are bound to it.
Example: A service used by multiple activities in the same app.
For Notes download BCA Resources App
Life Cycle of Services
In android, services have 2 possible paths to complete its life cycle namely Started and Bounded.
1. Started Service (Unbounded Service)
By following this path, a service will initiate when an application component calls the startService()
method. Once initiated, the service can run continuously in the background even if the component is
destroyed which was responsible for the start of the service.
Two option are available to stop the execution of service:
• By calling stopService() method,
• The service can stop itself by using stopSelf() method.
For Notes download BCA Resources App
Life Cycle of Services
2. Bounded Service:
It can be treated as a server in a client-server interface. By following this path, android
application components can send requests to the service and can fetch results. A
service is termed as bounded when an application component binds itself with a
service by calling bindService() method. To stop the execution of this service, all the
components must unbind themselves from the service by using unbindService()
method.
For Notes download BCA Resources App
Life Cycle of Services
For Notes download BCA Resources App
Life Cycle of Services
1. onCreate()
• The onCreate() method is the first method called when a service is
created.
• It initializes resources needed by the service, such as setting up
components or preparing data.
• This method runs only once throughout the service’s lifecycle, even if
the service is started multiple times.
For Notes download BCA Resources App
Life Cycle of Services
2. onStartCommand(Intent intent, int flags, int startId)
• The onStartCommand() method is called every time a service is
explicitly started using startService().
• It handles requests for starting a service and specifies how the system
should manage the service if it is terminated.
• The method returns an integer constant that determines the behavior
if the service is killed by the system
For Notes download BCA Resources App
Life Cycle of Services
3. onBind(Intent intent)
• The onBind() method is called when a component binds to the service
using bindService() instead of startService().
• It returns an IBinder object that clients use to interact with the
service.
• This method is essential for bound services but not required for
started services.
• If the service is not designed to support binding, it can return null.
For Notes download BCA Resources App
Life Cycle of Services
4. onUnbind(Intent intent)
• The onUnbind() method is called when all clients have disconnected
from a bound service.
• It allows the service to release any resources tied to binding.
• This method is only relevant for bound services.
For Notes download BCA Resources App
Life Cycle of Services
5. onRebind(Intent intent)
• The onRebind() method is called when a client rebinds to the service
after onUnbind() was previously called.
• It is a rare scenario and only applicable to bound services.
For Notes download BCA Resources App
Life Cycle of Services
6. onDestroy()
• The onDestroy() method is the final method called before a service is terminated.
• It is used for cleanup, such as stopping threads, releasing resources, and saving
final data.
• This method is called when:
• The service stops itself using stopSelf() or stopSelfResult().
• The service is explicitly stopped using stopService().
• The system kills the service to reclaim memory.
For Notes download BCA Resources App
Multimedia Framework
• The Multimedia Framework in Android provides a comprehensive set of APIs and
components that enable developers to create, manage, and manipulate audio,
video, and image content.
• It is designed to handle various multimedia tasks efficiently, offering support for a
wide range of media formats.
• This framework is essential for applications involving media playback, streaming,
and real-time processing.
• The architecture of the Android Multimedia Framework is layered, consisting of
different components that work together to deliver seamless multimedia
experiences.
For Notes download BCA Resources App
Common media codec formats used in Android
• H.264: Widely used video codec format that provides high-quality compression and is supported
by most modern devices and software.
• AAC: Popular audio codec format that provides high-quality compression and is widely supported
by devices and software.
• MP3: Well-known audio codec format that provides good compression and is supported by most
devices and software.
• VP9: Video codec format that provides high-quality compression and is supported by some
modern devices and software.
• JPEG: Image codec format that provides good compression and is widely supported by devices and
software.
• PNG: Image codec format That provides lossless compression which is supported by devices and
software.
For Notes download BCA Resources App
Multimedia Framework
For Notes download BCA Resources App
Multimedia Framework
• The Media Server creates the corresponding media service to the
multimedia application.
• The communication between the media server and Libmedia forms a
client-server model.
• The PV player processes the media data stream by demuxing the
media data to separate the video/audio data stream, decode
video/audio data, sync video, and audio time, and send the decoded
data out.
For Notes download BCA Resources App
Components of Android Multimedia Framework
1. Media Codec
It provides low-level access to hardware and software codecs for encoding and decoding
audio and video data.
Container: It is used to store audio file format on a system, where data can be manipulated
to reduce the size or change the quality of audio.
Audio Format: Any format or codec can be used including the ones provided by android
devices. However, it is recommended to use the specified file formats as per devices.
Network Protocol: Protocols supported in audio and video playback are RTSP, HTTP/HTTPS
progressive streaming, and live streaming draft protocol.
For Notes download BCA Resources App
Components of Android Multimedia Framework
2. Media Player
It is a component in the multimedia framework that provides high-level access to the media playback functionality of
Android that enables developers to play audio/video files and stream. It is also a core component of the Android multimedia
framework that enables developers to play audio and video files in their applications and provides a simple and flexible API
for playing media files from different sources. The sources are local files, network streams, and content providers. Media
player supports a wide range of audio and video formats which includes MP3, AAC, WAV, MPEG-4, H.264, etc.
Some key features of media players are:
• Playback control: This helps in controlling media files by providing a range of methods such as start(), pause(), stop(), and
seekTo().
• Playback State: This feature informs the developer about the playback state by providing functions that are onPrepared(),
onCompletion(), and onError().
• Audio Focus: This feature comes into the picture when multiple audio sources are playing simultaneously and the
developer has to manage all of it.
For Notes download BCA Resources App
Components of Android Multimedia Framework
3. Media Recorder
Provides high-level access to the media recording functionality of Android which allows developers to
capture audio/video data from a device’s microphone and camera. It provides a simple and flexible API
for recording media from different sources such as the device’s microphone or camera. Features of the
media recorder are:
• Recording control: This feature provides methods like start(), stop(), and reset() for controlling the
recording of media files.
• Recording state: This feature enables the user to notify about the state of recording by using methods
onInfo() and onError().
• Audio and Video Sources: This feature provides two methods setAudioSource and setVideoSource()
which enable developers to select appropriate audio and video sources for their recordings.
For Notes download BCA Resources App
Components of Android Multimedia Framework
4. Surface View
The surface provides a facility to play video content on android devices. It is a subclass
of the view class and provides a dedicated drawing surface for applications that need to
display video or graphics which are more complicated than simple views.
Feature of Surface View:
• Drawing surface: This feature is used by developers to draw complex graphics or
display video frames.
• Efficient rendering: This is used by developers when designing efficient rendering is
needed and provides better performance compared to other View classes when
rendering large images or video frames.
For Notes download BCA Resources App
Components of Android Multimedia Framework
5. Audio Manager
Controls the overall audio settings such as volume and routing. It allows developers to
manage audio settings and control audio playback for various applications and devices.
Functions of Audio Manager:
• Controlling Audio Volume
• Managing Audio Routing
• Handling Audio Focus
• Monitoring audio States
For Notes download BCA Resources App
Components of Android Multimedia Framework
6. Image Reader
Provides access to raw image data from a device’s camera or image sensors. It is a part
of the android Camera2 API and is available in Android API level 19 and higher.
Function that ImageReader class include:
• Capturing raw images
• Processing captured images
• Configuring capture settings
• Handling image buffers
For Notes download BCA Resources App