MAD notes
MAD notes
Android activities go through different states from the time they are created to when they
are destroyed. The system calls predefined lifecycle methods to manage these state
changes. The lifecycle of an activity is divided into several stages:
Lifecycle Methods:
1. onCreate(Bundle savedInstanceState)
Used to initialize UI elements, set up event listeners, and restore previous states
if available.
2. onStart()
o Called when the activity becomes visible but is not yet interactive.
3. onResume()
o Called when the activity is in the foreground and the user can interact with it.
4. onPause()
o Called when the activity is partially visible (e.g., when a new activity appears
on top).
5. onStop()
o Called when the activity is no longer visible (e.g., user navigates away).
o Used to save data and stop unnecessary processes like database access.
6. onDestroy()
o Called before the activity is completely removed from memory.
7. onRestart()
+--------------------+
+--------------------+
+--------------------+
+--------------------+
+--------------------+
+--------------------+
+--------------------+
+--------------------+
+--------------------+
+--------------------+
+--------------------+
| onDestroy() | → (Activity is removed from memory)
+--------------------+
What is RecyclerView?
RecyclerView is an advanced version of ListView used in Android to display large sets of data
efficiently. It improves performance by reusing views, reducing memory usage, and
supporting different layouts like linear, grid, and staggered layouts.
To use RecyclerView, you need to add its dependency in the Gradle file of your project. This
allows Android Studio to access the necessary libraries required for RecyclerView.
You need to add a RecyclerView widget in the XML layout file where you want to display the
list of items. It acts as a container to display the items dynamically.
A model class is required to represent the data that will be displayed in RecyclerView. This
class usually contains attributes such as text, images, or other data fields relevant to each
item in the list.
Each item displayed in the RecyclerView needs a separate XML layout file. This file defines
the structure of how each item will appear, such as having a TextView for titles or an
ImageView for pictures.
In the main activity or fragment, RecyclerView needs to be initialized. You also need to:
Once all the steps are completed, running the application will display a scrollable list of
items. If configured correctly, RecyclerView will efficiently handle large datasets with
smooth scrolling.
Animations: Add smooth transition effects when items are added or removed.
Pagination: Load more data when the user scrolls to the bottom.
For example, an intent can be used to open a new activity, share data, make a call, or open
a web page.
Types of Intents
1. Explicit Intent
Used to launch a specific component (such as an activity or service) within the same
application.
2. Implicit Intent
Used when the destination component is not specified directly. Instead, the system
decides which app or component can handle the request.
3. Broadcast Intent
4. Pending Intent
A special type of intent that allows another app or system service to execute an
action on behalf of your app.
What is AsyncTask?
AsyncTask was a background processing mechanism in Android used for performing short-
duration tasks like downloading data or accessing a database without freezing the UI.
However, AsyncTask is now deprecated (from Android 11) and replaced by alternatives like
Executors, Handlers, or Kotlin Coroutines.
Features of AsyncTask:
Lifecycle of AsyncTask:
4. onPostExecute() – Executes after the background task is completed, updating the UI.
Alternatives:
To access the internet, you must declare the permission in the AndroidManifest.xml file.
Android does not allow network operations on the main thread, so a separate thread or
library is required. Some popular options:
You can fetch data from a server using APIs (Application Programming Interfaces), such as
fetching weather data or user profiles from an online database.
The received data (usually in JSON format) needs to be parsed and displayed in the UI using
RecyclerView or other views.
No internet connection
Server errors
Introduction
Mobile application development has evolved significantly over the years, from simple pre-
installed apps on early mobile phones to the advanced, AI-driven apps we use today. The
journey of mobile app development can be divided into several key phases.
In the early days of mobile technology, phones had pre-installed applications such as
calculators, calendars, and simple games (like Snake on Nokia phones).
These apps were basic, operated on low-processing power devices, and could not be
modified or downloaded.
Java 2 Micro Edition (J2ME) became a popular platform for mobile apps, enabling
third-party applications to be installed on feature phones.
WAP (Wireless Application Protocol) was used for basic mobile internet access, but it
was slow and limited.
2007: Apple launched the first iPhone, introducing iOS and the concept of a modern
touchscreen interface.
2008: Apple App Store was introduced, allowing users to download and install apps
from a centralized marketplace.
2008: Google launched Android OS, leading to the creation of the Google Play Store
(formerly Android Market).
Developers started creating native applications for iOS and Android using Objective-
C (iOS) and Java (Android).
Mobile gaming became a billion-dollar industry, with games like Angry Birds and
Candy Crush gaining massive popularity.
Introduction of mobile payments (Apple Pay, Google Wallet) and social media apps
(Instagram, Snapchat).
Introduction of AI & Machine Learning: Apps like Google Assistant and Siri became
more advanced.
Rise of Flutter and React Native: Developers can now build cross-platform apps
efficiently.
5G and Progressive Web Apps (PWAs): Faster networks allow web-based apps to
function like native apps.
Internet of Things (IoT) apps: Apps now connect to smart home devices, wearables,
and cars.
History of Android
Introduction
Android is the world’s most popular mobile operating system, powering billions of devices.
Developed by Android Inc. and later acquired by Google, Android has evolved from a simple
mobile OS to a powerful platform supporting smartphones, tablets, smart TVs, wearables,
and IoT devices.
Founded in 2003 by Andy Rubin, Rich Miner, Nick Sears, and Chris White to develop
a smart operating system for cameras.
Later, they shifted their focus to mobile phones to compete with Symbian and
Windows Mobile.
2005: Google acquired Android Inc. for $50 million, and Android became a Google
project.
2008: The first Android phone, HTC Dream (T-Mobile G1), was launched with
Android 1.0. It featured:
2009: Android 1.6 Donut added CDMA support, allowing Android phones on more
networks.
2010: Android 2.2 Froyo introduced hotspot tethering and Flash support in
browsers.
2011: Android 3.0 Honeycomb was made for tablets, first seen on the Motorola
Xoom.
2014: Android 5.0 Lollipop introduced Material Design UI, a new look for Android.
2015: Android 6.0 Marshmallow brought app permissions and Google Now on Tap.
2017: Android 8.0 Oreo improved battery life with Doze mode.
2018: Android 9.0 Pie introduced gesture navigation and adaptive battery.
2019: Android 10 removed dessert names and introduced dark mode and full
gesture navigation.
2022: Android 13 improved security, per-app language settings, and better tablet
support.
o 2008: Apple App Store and Android Market (now Google Play Store) were
introduced.
1. Native Apps
2. Web Apps
Require an internet connection and are built using HTML, CSS, and JavaScript.
3. Hybrid Apps
Work like web apps but offer offline capabilities and push notifications.
o 2007: Google launched Android OS and the Open Handset Alliance (OHA).
o 2008: First Android phone, HTC Dream (T-Mobile G1), was released.
2. Customizable UI
Material You (Android 12) introduced dynamic themes based on wallpaper colors.
3. Multi-Tasking
Features like App Permissions, Play Protect, Encrypted Data Storage, and Biometric
Authentication.
Activities: Represent a single screen with a user interface (e.g., Login Screen, Home
Screen).
Broadcast Receivers: Handle system-wide events like battery low, network change, or
incoming SMS.
Content Providers: Manage access to shared app data (e.g., contacts, media files,
databases).
2. Go to About Phone and tap Build Number 7 times to enable Developer Options.
Download and install OEM USB drivers from the manufacturer’s website (for
Samsung, Xiaomi, etc.).
3. Connect the Device to the Computer
3. The connected Android device should appear. If not, type the following command in
Command Prompt/Terminal:
4. adb devices
5. If the device appears, the setup is complete, and you can run your app on the real
device.
Android Terminologies
1. Activity
An Activity represents a single screen in an Android app. It manages the UI and user
interactions. Example: A login screen or a home screen.
2. Intent
3. Service
A Service is a background process that runs without a UI, such as playing music, fetching
notifications, or syncing data.
4. Broadcast Receiver
A Broadcast Receiver listens for system-wide or app-specific events, like battery low,
network change, or SMS received.
5. Content Provider
A Content Provider manages access to structured data (like contacts, media files, or
databases) and allows apps to share data securely.
6. Fragment
A Fragment is a modular UI component that can be used inside an activity. It allows for
flexible UI design, especially in tablets.
8. RecyclerView
9. Gradle
Gradle is the build system for Android projects. It manages dependencies, compiles code,
and builds APKs.
This file contains app permissions, activity declarations, services, and broadcast receivers.
ADB is a command-line tool used to communicate with Android devices for debugging,
installing APKs, and accessing the shell.
ANR occurs when an app freezes for too long (more than 5 seconds) due to long-running
tasks on the main thread.
Google’s UI design language that ensures a consistent and modern look across Android
apps.
Android Components
Android applications are built using four main components, each serving a specific purpose
in the app’s functionality.
1. Activities
It handles the UI and user interactions (e.g., login screen, home screen).
2. Services
Example: A music player app runs a background service to play songs even when the app is
closed.
3. Broadcast Receivers
Events can be battery low, network change, SMS received, boot completed, etc.
4. Content Providers
A Content Provider manages shared app data and allows other apps to access it
securely.
Used for sharing structured data like contacts, media files, and databases.
Example: The Contacts app provides access to phone numbers that can be used by
WhatsApp or Telegram.
Additional Components
5. Fragments
6. Intent
Intent is used for communication between different app components (e.g., launching
an activity or a service).
Implicit Intent: The system chooses the appropriate component based on intent
action.
What is an Intent?
Send a broadcast
Types of Intents
✅ 1. Explicit Intent
It is mainly used within the same application to start a specific activity or service.
Characteristics:
✅ 2. Implicit Intent
Instead, it declares a general action to perform, and the system decides which
component (from your app or other apps) can handle it.
Characteristics:
Requires intent filters to be declared in the manifest for the receiving app
Android Activity and Its Lifecycle (with Callback Methods & Diagram)
An Activity in Android represents a single screen with a user interface. It acts as an entry
point for user interaction in an app. For example, the Login page, Home screen, or Settings
screen in an app are all activities.
The Android system manages the lifecycle of each activity using a series of callback
methods. These methods help handle transitions between different states like creation,
running, paused, stopped, and destroyed.
onCreate() Called when the activity is first created. Initialize UI, data, etc.
onStart() Called just before the activity becomes visible to the user.
onResume() Called when the activity starts interacting with the user.
Called when the activity is coming back to the foreground after being
onRestart()
stopped.
onDestroy() Called when the activity is destroyed and removed from memory.
+-------------------+
| onCreate() |
+-------------------+
+-------------------+
| onStart() |
+-------------------+
+-------------------+
| onResume() |
+-------------------+
+-------------------+
| onPause() |
+-------------------+
+-------------------+
| onStop() |
+-------------------+
+-------------------+
| onDestroy() |
+-------------------+
↑ ↓
+------------+ +----------------+
+------------+ +----------------+
📝 Summary
A bridge between the hardware and higher-level Android runtime and framework.
Android Runtime (ART) executes app code and replaces the older Dalvik VM.
Core libraries provide Java-like classes (data structures, file access, network, etc.)
These are pre-built native libraries written in C/C++, which provide support for media
playback, database handling, graphics rendering, etc.
📌 Popular Libraries:
🔹 5. Application Framework
📌 Important Components:
Activity Manager
Window Manager
Content Providers
Package Manager
This is where user-installed apps and built-in apps (like Dialer, SMS, Camera) exist.
Built using Java/Kotlin + XML UI and use the framework APIs underneath.
+--------------------------+
| Applications |
+--------------------------+
| Application Framework |
+--------------------------+
| Android Runtime (ART) |
| + Native Libraries |
+--------------------------+
| Hardware Abstraction |
+--------------------------+
| Linux Kernel |
+--------------------------+
📱 What is a Fragment?
👉 Think of a fragment as a mini-activity that has its own lifecycle and layout but lives inside
an activity.
Reusable UI components
🔁 Fragment Lifecycle
Just like an activity, a fragment has its own lifecycle, which is closely tied to the activity’s
lifecycle. Here's a breakdown of important lifecycle methods:
🔹 1. onAttach(Context context)
This is where you return the view for your fragment UI.
🔹 5. onStart()
🔹 6. onResume()
🔹 7. onPause()
🔹 8. onStop()
🔹 10. onDestroy()
🔹 11. onDetach()
onAttach()
onCreate()
onCreateView()
onViewCreated()
onStart()
onResume()
onPause()
onStop()
onDestroyView()
↑
onDestroy()
onDetach()
In Android, handling a button click means responding to the user's action when they tap on
a button in your app's user interface. This is one of the most common and important
interactions in any app.
This makes the button visible on the screen and identifiable in your code.
In your Java or Kotlin activity, you reference the button using its ID so you can
interact with it in code.
✅ 3. Set an OnClickListener
When the button is clicked, the listener triggers a function or action (like showing a
message, opening a new screen, or performing calculations).
📋 Ways to Handle Click Events
🔹 1. Using setOnClickListener
Declare the function name in XML and define that function in your activity.
Fetching data
In Android, Views and ViewGroups form the building blocks of the user interface (UI). They
work together to create and organize everything the user sees and interacts with on the
screen.
📌 What is a View?
A View is the basic UI element in Android that represents a rectangular area on the screen.
It is used to display content or capture user input.
🧩 Examples of Views:
📌 What is a ViewGroup?
A ViewGroup is a container that holds one or more Views or other ViewGroups. It defines
how child views are arranged on the screen.
🧩 Examples of ViewGroups:
✅ Features:
ViewGroups can contain multiple Views, while Views are leaf nodes (they cannot
contain other views).
📊 Example UI Structure:
<LinearLayout> ← ViewGroup
📝 Summary
In Android, background tasks are operations that run in the background thread instead of
the main (UI) thread, to keep the app responsive and smooth for the user.
o Downloading/uploading files
o Reading/writing to database
o Heavy computations
Executors
⚙️What is AsyncTask?
⚠️Limitations of AsyncTask
6. No Error Handling:
Doesn’t have built-in support for exceptions or retries.