Android Components Explained: Activities, Intents, Fragments, Services & More

 

Unit 4 · Android Development

Android Components:
The Big Picture

The core players behind every Android app — Activities, Services, Intents, Fragments, Broadcast Receivers, and Content Providers.

📱
Activities
✉️
Intents
🧩
Fragments
⚙️
Services
📡
Broadcasts
🗄️
Content Providers
01

What Are Android Components?

Android components are the core players that make up every Android app. Unlike other platforms, Android doesn't have a single main() function as a starting point — the system launches components based on user actions or system events.

💡
Key insight: Components are activated by special messages called Intents. Each component serves a distinct role and keeps Android apps modular, responsive, and independently testable.
02

Activities: The Building Blocks of UI

An Activity represents a single screen with which users can interact. Every distinct UI screen in an Android app — a login form, a gallery view, a settings page — is typically part of an Activity. They act as entry points for user interactions.

Activity Lifecycle

The Activity lifecycle dictates how an Android app responds to changes like incoming calls, navigating away, or device rotation. Managing state during these callbacks is vital for a smooth user experience.

onCreate()
Initial setup, inflate layouts
onStart()
Activity becomes visible
onResume()
User can now interact
onPause()
Loses foreground focus
onStop()
No longer visible
onDestroy()
System reclaims memory
class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        // Initialize UI and variables here
    }

    override fun onPause() {
        super.onPause()
        // Save state before losing focus
    }
}
03

Intents: Messages Between Components

Intents are Android's messaging system that lets components communicate with each other — even across different apps. They are essential for navigating between screens and requesting actions.

EXPLICIT
Named Target
Directly names the component to launch. Used when moving from a login screen to a dashboard within your own app.
IMPLICIT
Action-Based
Describes an action like "VIEW" or "SEND" without a specific target. The system finds a matching handler automatically.
// Explicit Intent — go to a specific screen
val intent = Intent(this, DashboardActivity::class.java)
startActivity(intent)

// Implicit Intent — open a URL in any browser
val webIntent = Intent(Intent.ACTION_VIEW,
    Uri.parse("https://example.com"))
startActivity(webIntent)
04

Fragments: Reusable UI Modules

A Fragment is like a miniature Activity — a portion of UI that can be reused across different activities. It has its own lifecycle, manages its own layout, and is especially powerful on larger devices like tablets with dual-panel layouts.

Activity
Full Screen
  • Independent entry point
  • Managed by the OS
  • One Activity per screen
  • Heavier lifecycle
Fragment
Partial UI
  • Must be hosted by Activity
  • Reusable across screens
  • Multiple per Activity
  • Flexible & modular

Fragment Lifecycle

Fragments have lifecycle callbacks similar to Activities, but slightly more complex due to their dependency on the host Activity:

// Key Fragment lifecycle methods
onAttach()       // Fragment attached to Activity
onCreateView()  // Inflate the fragment's layout
onStart()       // Fragment becomes visible
onPause()       // User leaves the fragment
onDestroyView() // View hierarchy removed
🧩
Design strength: Fragments cannot exist on their own — they must be hosted by an Activity. This dependency is a strength: it allows developers to design flexible UIs without maintaining multiple full-screen Activities.
05

Services: Background Champions

Services are components that perform long-running tasks in the background without a user interface — music playback, data sync, or handling uploads. They keep working even after the user navigates away.

▶️
Started Service
Begins when a component calls startService() and runs until explicitly stopped. Independent of the caller's lifecycle.
🔗
Bound Service
Allows other components to bind and interact with it. Stays alive as long as at least one component is bound.
🔔
Foreground Service
Runs with a user-visible notification and is less likely to be killed by the system — ideal for music players or GPS tracking.
06

Broadcast Receivers: Event Listeners

Broadcast Receivers are Android's way of listening for and responding to system-wide or app-specific events. When the specified event occurs, Android delivers a broadcast Intent to all matching receivers.

📶
System Event
Connectivity Change
React when the device goes online or offline to sync data or alert the user.
💬
System Event
SMS Received
Intercept incoming messages to trigger in-app notifications or actions.
🔋
System Event
Battery Change
Monitor battery levels to adjust app behavior or notify the user.
07

Content Providers: Shared Data Managers

When an Android app needs to share structured data with other apps — such as contacts or media — the system uses Content Providers. They offer a standard API to query, insert, and update data, accessible to other apps with proper permission.

📖
Classic example: The Android Contacts app exposes data through a Content Provider. Many apps read or write contact info via ContentResolver — a clean, permission-controlled interface without direct database access.
// Querying the Contacts Content Provider
val cursor = contentResolver.query(
    ContactsContract.Contacts.CONTENT_URI,
    null, null, null, null
)
cursor?.use {
    while (it.moveToNext()) {
        // Process each contact row
    }
}

Topics covered in this unit:

Activities Activity Lifecycle Explicit Intents Implicit Intents Fragments Fragment Lifecycle Started Services Bound Services Foreground Services Broadcast Receivers Content Providers

Conclusion

Android components are the foundation of every Android application. By mastering Activities, Lifecycle handling, Intents, Fragments, Services, Broadcast Receivers, and Content Providers, you unlock the ability to build feature-rich, responsive, and scalable apps.

Each component has a unique purpose. Understanding how they interact enables you to architect clean, efficient, and maintainable applications.

"Understanding how components connect
is what separates good apps from great ones."

FAQ

Frequently Asked Questions

Can any component be launched without Intents?
Most components — like Activities, Services, and Broadcast Receivers — are activated through Intents. Content Providers are accessed differently, via a ContentResolver.
What distinguishes an explicit intent from an implicit one?
Explicit intents specify the exact component to start. Implicit intents describe the action to perform and let the system find a matching handler from any installed app.
How is a Fragment different from an Activity?
Fragments are reusable UI parts that must be hosted within an Activity — they cannot operate independently. This makes them more modular and flexible than standalone Activities.
Why is the Activity lifecycle important?
Understanding the lifecycle ensures your app handles state changes smoothly — whether it's a user navigating away, receiving a call, or rotating the device.
When should I use a Service?
Use a Service when a task must continue running outside of the UI thread or after the user has left your app — such as music playback, file uploads, or background data syncing.


Post a Comment

0 Comments