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:

  1. App calls startActivity(intent) which goes through Instrumentation
  2. Instrumentation sends the Intent to ActivityManagerService (AMS) via Binder
  3. AMS queries PackageManagerService (PMS) to resolve the Intent
  4. PMS checks all registered IntentFilters from AndroidManifest files
  5. If multiple matches → chooser dialog. If one match → direct launch.
  6. AMS creates/reuses an ActivityRecord and starts the target Activity

Intent Flags (critical for interview):

  • FLAG_ACTIVITY_NEW_TASK — Start Activity in a new task
  • FLAG_ACTIVITY_CLEAR_TOP — Clear Activities above the target in the stack
  • FLAG_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

codeTap to expand ⛶
1// Explicit Intent — within your app
2val intent = Intent(this, DetailActivity::class.java).apply {
3 putExtra("NOTE_ID", noteId)
4 putExtra("EDIT_MODE", true)
5}
6startActivity(intent)
7
8// Implicit Intent — cross-app communication
9val 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"))
14
15// 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_IMMUTABLE
23)
24
25// Deep Link handling in Activity
26class DeepLinkActivity : AppCompatActivity() {
27 override fun onCreate(savedInstanceState: Bundle?) {
28 super.onCreate(savedInstanceState)
29 handleDeepLink(intent)
30 }
31
32 override fun onNewIntent(intent: Intent) {
33 super.onNewIntent(intent)
34 handleDeepLink(intent) // Called when singleTop and already running
35 }
36
37 private fun handleDeepLink(intent: Intent) {
38 val uri = intent.data ?: return
39 when {
40 uri.pathSegments.contains("note") -> {
41 val noteId = uri.lastPathSegment
42 navigateToNote(noteId)
43 }
44 uri.pathSegments.contains("share") -> {
45 val token = uri.getQueryParameter("token")
46 handleShareToken(token)
47 }
48 }
49 }
50}
51
52// 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:

  1. Explain the difference between explicit and implicit intents with use cases
  2. What happens internally when startActivity() is called? Trace through Binder IPC
  3. Implement a custom implicit intent with an IntentFilter in the manifest
  4. Explain PendingIntent flags — FLAG_UPDATE_CURRENT vs FLAG_CANCEL_CURRENT
  5. Set up App Links with Digital Asset Links verification
  6. 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.