Prepared By : Prof. Uday Shah (HOD-IT)
Unit 3 : Application Development
Contents
9.
Working with Integer Resources
1. Context
- Context
represents the current state of the application or object.
- It
provides access to system services like LayoutInflater, Location,
Notification, etc.
- Used
to access application resources such as strings, colors, and images.
- Context
is required to start activities, services, and broadcast receivers.
- Two
main types: Application Context and Activity Context.
- Application
context lives as long as the app is running; activity context lasts only
for an activity’s life.
- Used
to access shared preferences, databases, and file system.
- Often
passed to adapters, dialogs, and UI components.
- Essential
for performing operations that require global information.
- Misuse
of context can cause memory leaks, especially in long-lived
objects.
- It
helps apps communicate with Android OS.
- Activity,
Service, and Application classes all extend Context.
- Plays
a crucial role in resource management and navigation.
2. Activity
- Activity
represents a single screen in an Android application.
- Each
screen in an app (like login, dashboard) is an Activity.
- Must
extend the AppCompatActivity or Activity class.
- Activities
interact with users through UI components.
- Lifecycle
methods: onCreate(), onStart(), onResume(), onPause(), onStop(), onDestroy().
- The OS
may destroy and recreate activities depending on memory needs.
- Activities
can start other activities using intents.
- Data
can be passed between activities using Intent.putExtra().
- Activities
can return results using setResult().
- Layout
is attached via setContentView().
- Each
activity must be declared in AndroidManifest.xml.
- Activities
may operate in foreground, background, or stopped
state.
- Crucial
for user interaction flow of an app.
3. Intent
- Intent
is a messaging object used to communicate between components.
- Two
types: Explicit (open specific component) and Implicit (open
external apps).
- Used
to start activities, services, and broadcast receivers.
- Can
carry data using extras (putExtra).
- Implicit
intents use action, data, and category to find
suitable apps.
- Useful
for tasks like opening camera, sending email, or making a call.
- Intents
support inter-app communication.
- startActivity()
launches new activities using intents.
- startActivityForResult()
returns results from child activities.
- Intents
can trigger system components like alarms or notifications.
- Help
maintain modularity and reusability in apps.
- Can
pass serializable or parcelable objects.
- Important
for navigation and task flows.
4. Service
- A
service performs long-running operations in the background.
- Does
not provide a user interface.
- Examples:
playing music, downloading files, GPS tracking.
- Services
run even when the activity is closed.
- Types:
Foreground Service, Background Service, Bound Service.
- Foreground
service shows a notification and cannot be killed easily.
- Bound
service allows communication between service and activity.
- Lifecycle
methods: onCreate(), onStartCommand(), onBind(), onDestroy().
- Useful
for tasks that must continue independently of UI.
- Services
are declared in AndroidManifest.xml.
- Must
handle memory efficiently to avoid app crashes.
- WorkManager
or JobScheduler can schedule service-like tasks.
- Improves
performance by separating heavy tasks from UI.
5. Broadcast
Receiver
- Receives
and responds to broadcast messages from the system or apps.
- Examples:
battery low, SMS received, Wi-Fi on/off.
- Two
types: Static Receiver and Dynamic Receiver.
- Static
receivers declared in manifest; dynamic receivers registered in code.
- onReceive()
method is triggered when broadcast arrives.
- Lightweight
and should not perform heavy tasks.
- Used
to trigger background processes.
- Commonly
used in alarms, notifications, and system event monitoring.
- Receivers
can filter broadcasts using intent filters.
- Used
for inter-component communication.
- Helps
apps react instantly to system-level changes.
- Ideal
for short operations like updating UI or saving data.
- Must
be used carefully to avoid performance issues.
6. Fragment
- A
Fragment is a reusable portion of UI within an Activity.
- Helps
create flexible and modular UIs.
- Commonly
used for tablet and phone layouts.
- Each
fragment has its own lifecycle and UI but depends on activity.
- Declared
by extending Fragment or FragmentCompat.
- Can be
reused across multiple activities.
- Fragments
can be replaced dynamically at runtime.
- Usually
placed inside a container like FrameLayout.
- Communicates
with activity using interfaces or shared ViewModel.
- Good
for multi-pane layouts (e.g., Master–Detail).
- Helps
reduce code duplication.
- Supports
back stack operations.
- Allows
adaptive UI design.
7. Fragment
Lifecycle
- Fragment
has a more complex lifecycle than Activity.
- Key
methods: onAttach(), onCreate(), onCreateView(), onStart(), onResume().
- UI is
created in onCreateView() using XML layout.
- onPause(),
onStop(), and onDestroyView() handle cleanup.
- onDestroy()
removes fragment resources from memory.
- onDetach()
disconnects fragment from activity.
- Lifecycle
allows dynamic UI updates.
- Helps
manage memory by creating/destroying UI as needed.
- Fragment
lifecycle is tied with activity lifecycle.
- Handling
lifecycle properly prevents crashes and memory leaks.
- Ideal
for dynamic screens inside activities.
8. Working
with Strings
- Strings
are stored in res/values/strings.xml.
- Helps
maintain consistency of text across the app.
- Allows
localization (multi-language support).
- Used
in layouts using @string/your_string.
- Prevents
hardcoding of text in code.
- Reduces
maintenance effort.
- Supports
placeholders and formatting.
- Can
be retrieved in code using getString().
- Helps
create cleaner and scalable apps.
9. Working
with Integer Resources
- Stored
in integers.xml file inside values folder.
- Useful
for constant numeric values such as limits or settings.
- Avoids
hardcoded numbers in code.
- Can be
accessed using resources.getInteger().
- Helps
maintain organized resource structure.
- Useful
in themes, layouts, and animations.
- Improves
code flexibility.
10. Working
with Drawable
- Drawable
resources represent images, icons, shapes.
- Stored
in drawable folders (drawable, drawable-hdpi, etc.).
- Supports
PNG, JPEG, SVG, XML shape files.
- Used
for backgrounds, buttons, logos, etc.
- Can
create custom shapes using XML (rectangle, gradient).
- Accessed
in XML using @drawable/name.
- Scaled
automatically based on screen density.
- Lightweight
and easy to manage.
11. Working
with Color
- Stored
in colors.xml in values folder.
- Define
theme colors, UI element colors, text colors, etc.
- Helps
maintain consistency across screens.
- Supports
RGB, HEX, and Material colors.
- Used
in XML via @color/colorName.
- Works
with dark mode themes.
- Can
be accessed in Kotlin using ContextCompat.getColor().
- Centralized
color control improves design.
12. Working
with Style
- Styles
define visual appearance of UI elements.
- Stored
in styles.xml.
- Can
style buttons, text, layouts, etc.
- Helps
maintain consistent UI.
- Supports
inheritance of styles.
- Can
create themes for entire application.
- Reduces
repetitive XML code.
- Works
with Material Design components.
13.
Typography
- Typography
defines text appearance, including font style, weight, and size.
- Android
supports custom fonts using font/ folder.
- Material
Typography guidelines help maintain readability.
- Fonts
can be applied using XML or themes.
- Supports
downloadable Google Fonts.
- Helps
create modern, professional UI.
- Consistent
typography improves user experience.
14. Material
Design
- Google's
design system for Android applications.
- Provides
guidelines for layout, spacing, colors, buttons, etc.
- Components
include MaterialButton, CardView, AppBar, BottomNavigation.
- Ensures
consistent, intuitive user experience.
- Provides
motion guidelines with transitions and animations.
- Uses
elevation and shadows to show depth.
- Supports
light and dark themes.
- Enhances
accessibility and usability.
- Helps
build modern, attractive interfaces.