Intent System & Inter-Component Communication
📖 Concept
Intents are the messaging system that connects Android components. At the senior level, you need to understand how Intents work at the framework and Binder IPC level.
Types of Intents:
- Explicit Intent — Target a specific component by class name. Used within your app.
- Implicit Intent — Describe an action + data, let the system find a matching component. Used for cross-app communication.
How Intent resolution works internally:
- App calls
startActivity(intent)which goes through Instrumentation - Instrumentation sends the Intent to ActivityManagerService (AMS) via Binder
- AMS queries PackageManagerService (PMS) to resolve the Intent
- PMS checks all registered IntentFilters from AndroidManifest files
- If multiple matches → chooser dialog. If one match → direct launch.
- AMS creates/reuses an ActivityRecord and starts the target Activity
Intent Flags (critical for interview):
FLAG_ACTIVITY_NEW_TASK— Start Activity in a new taskFLAG_ACTIVITY_CLEAR_TOP— Clear Activities above the target in the stackFLAG_ACTIVITY_SINGLE_TOP— Reuse the existing instance if it's on top (calls onNewIntent)FLAG_ACTIVITY_NO_HISTORY— Activity won't stay in the back stack
PendingIntent — A token that grants another app permission to execute an Intent on your behalf. Used in notifications, alarms, widgets. Security-critical: use FLAG_IMMUTABLE (API 31+).
Deep Links & App Links:
- Deep links: custom scheme (myapp://path) — any app can register
- App Links: verified HTTPS links — only your app can handle (requires Digital Asset Links verification)
💻 Code Example
1// Explicit Intent — within your app2val intent = Intent(this, DetailActivity::class.java).apply {3 putExtra("NOTE_ID", noteId)4 putExtra("EDIT_MODE", true)5}6startActivity(intent)78// Implicit Intent — cross-app communication9val shareIntent = Intent(Intent.ACTION_SEND).apply {10 type = "text/plain"11 putExtra(Intent.EXTRA_TEXT, "Check out this note!")12}13startActivity(Intent.createChooser(shareIntent, "Share via"))1415// PendingIntent for notifications (API 31+ requires FLAG_IMMUTABLE)16val pendingIntent = PendingIntent.getActivity(17 context,18 requestCode,19 Intent(context, DetailActivity::class.java).apply {20 putExtra("NOTE_ID", noteId)21 },22 PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE23)2425// Deep Link handling in Activity26class DeepLinkActivity : AppCompatActivity() {27 override fun onCreate(savedInstanceState: Bundle?) {28 super.onCreate(savedInstanceState)29 handleDeepLink(intent)30 }3132 override fun onNewIntent(intent: Intent) {33 super.onNewIntent(intent)34 handleDeepLink(intent) // Called when singleTop and already running35 }3637 private fun handleDeepLink(intent: Intent) {38 val uri = intent.data ?: return39 when {40 uri.pathSegments.contains("note") -> {41 val noteId = uri.lastPathSegment42 navigateToNote(noteId)43 }44 uri.pathSegments.contains("share") -> {45 val token = uri.getQueryParameter("token")46 handleShareToken(token)47 }48 }49 }50}5152// Navigation Component deep link (modern approach)53// In nav_graph.xml:54// <deepLink app:uri="myapp://note/{noteId}" />55// Handles argument extraction automatically
🏋️ Practice Exercise
Practice:
- Explain the difference between explicit and implicit intents with use cases
- What happens internally when startActivity() is called? Trace through Binder IPC
- Implement a custom implicit intent with an IntentFilter in the manifest
- Explain PendingIntent flags — FLAG_UPDATE_CURRENT vs FLAG_CANCEL_CURRENT
- Set up App Links with Digital Asset Links verification
- What are the security implications of exported Activities?
⚠️ Common Mistakes
Not using FLAG_IMMUTABLE for PendingIntents on API 31+ — causes a crash
Passing large data through Intent extras — Bundle has a ~500KB limit via Binder. Use content URIs or a shared database instead
Not handling onNewIntent for singleTop/singleTask launch modes — deep links won't update the UI
Exporting Activities unintentionally — any app can launch your exported Activity, which is a security risk
💼 Interview Questions
🎤 Mock Interview
Mock interview is powered by AI for Intent System & Inter-Component Communication. Login to unlock this feature.