Intent
Android
Extras
Android Development
Programming

Listing all extras of an Intent

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

Android Intent extras are stored in a Bundle, which means you can inspect them dynamically instead of fetching each key by hand. That is especially useful when debugging deep links, broadcast receivers, and activity launches where you are not fully sure what another component attached.

The Basic Way to List All Extras

The core API is intent.extras, which returns a Bundle?. From there, iterate through keySet() and read each value.

kotlin
1val extras = intent.extras
2
3if (extras == null) {
4    println("No extras found")
5} else {
6    for (key in extras.keySet()) {
7        val value = extras.get(key)
8        println("$key = $value")
9    }
10}

This works for quick inspection because Bundle.get returns Any?, which is enough for logging and debugging.

A More Complete Debug Helper

In real applications, a helper function is better than repeating the loop in every activity.

kotlin
1import android.content.Intent
2import android.os.Bundle
3import android.util.Log
4
5fun dumpExtras(intent: Intent, tag: String = "IntentDebug") {
6    val extras: Bundle? = intent.extras
7
8    if (extras == null) {
9        Log.d(tag, "No extras present")
10        return
11    }
12
13    for (key in extras.keySet()) {
14        val value = extras.get(key)
15        val type = value?.javaClass?.simpleName ?: "null"
16        Log.d(tag, "$key = $value ($type)")
17    }
18}

Usage:

kotlin
1override fun onStart() {
2    super.onStart()
3    dumpExtras(intent)
4}

Logging both the value and the runtime type makes debugging much easier when different callers pass different data shapes.

Handling Typed Values Correctly

For inspection, extras.get(key) is fine. For application logic, keep using the typed getters such as getString, getInt, and getBoolean.

kotlin
val username = intent.getStringExtra("username")
val retryCount = intent.getIntExtra("retry_count", 0)
val isAdmin = intent.getBooleanExtra("is_admin", false)

The difference matters because typed getters express intent and provide default handling. A debug dump should be generic, but business logic should not depend on raw Any? access unless that is truly necessary.

Extras Can Contain More Than Strings

A Bundle can hold many kinds of values:

  • Primitive values such as Int, Boolean, and Long.
  • Strings and string arrays.
  • 'Parcelable objects.'
  • 'Serializable objects.'
  • Nested Bundle values.

If you want a more readable debug dump, handle nested Bundle values explicitly:

kotlin
1fun dumpBundle(bundle: Bundle, indent: String = "") {
2    for (key in bundle.keySet()) {
3        val value = bundle.get(key)
4        when (value) {
5            is Bundle -> {
6                println("${indent}$key = Bundle")
7                dumpBundle(value, indent + "  ")
8            }
9            else -> {
10                println("${indent}$key = $value")
11            }
12        }
13    }
14}

That is useful when extras contain structured payloads instead of flat keys.

This technique is not limited to activities. The same pattern works in services, broadcast receivers, and fragments that receive arguments converted from an intent.

For a BroadcastReceiver:

kotlin
override fun onReceive(context: Context, intent: Intent) {
    dumpExtras(intent, "ReceiverExtras")
}

For debugging external integrations, this is often faster than guessing which keys another app uses.

Why Null Checks Matter

intent.extras can be null. Even if you expect extras, defensive checks are still worth it because:

  • The activity may have been launched from a different path.
  • A deep link may omit optional parameters.
  • An integration may change over time.

A missing Bundle is normal in many flows, so treat it as a valid case rather than as an exception.

Common Pitfalls

  • Assuming every intent has extras and skipping the null check.
  • Using the generic Bundle.get values for application logic instead of typed getters.
  • Forgetting that complex objects may be Parcelable or nested Bundle values.
  • Logging extras in production builds without thinking about sensitive information.
  • Debugging only the expected keys and missing a typo or alternate key name sent by another component.

Summary

  • Intent extras live inside a Bundle and can be listed by iterating over keySet().
  • 'Bundle.get(key) is useful for debugging, while typed getters are better for application logic.'
  • A reusable helper makes intent inspection much easier across activities and receivers.
  • Nested bundles and parcelables may require slightly richer logging.
  • Always null-check intent.extras, because missing extras are a normal runtime case.

Course illustration
Course illustration

All Rights Reserved.