Android Development
Intents
putExtra
getExtra
String Data Handling

How to use putExtra and getExtra for string data

Master System Design with Codemia

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

Introduction

In Android, Intent extras are the standard way to pass small pieces of data from one activity to another. For string values, the sender usually uses putExtra, and the receiver reads the same key with getStringExtra.

Put a String into an Intent

Suppose MainActivity needs to launch DetailActivity and send a username:

kotlin
1import android.content.Intent
2import androidx.appcompat.app.AppCompatActivity
3
4class MainActivity : AppCompatActivity() {
5
6    fun openDetails() {
7        val intent = Intent(this, DetailActivity::class.java)
8        intent.putExtra(EXTRA_USERNAME, "mark")
9        startActivity(intent)
10    }
11
12    companion object {
13        const val EXTRA_USERNAME = "extra_username"
14    }
15}

putExtra stores the string under a key. That key must match exactly when the receiving activity reads it.

Read the String with getStringExtra

On the receiving side:

kotlin
1import android.os.Bundle
2import android.widget.TextView
3import androidx.appcompat.app.AppCompatActivity
4
5class DetailActivity : AppCompatActivity() {
6
7    override fun onCreate(savedInstanceState: Bundle?) {
8        super.onCreate(savedInstanceState)
9        setContentView(R.layout.activity_detail)
10
11        val username = intent.getStringExtra(MainActivity.EXTRA_USERNAME)
12        findViewById<TextView>(R.id.usernameView).text = username ?: "unknown"
13    }
14}

getStringExtra returns a nullable string, so the receiver should handle the case where the extra is missing.

Why getStringExtra Is Better Than a Generic Getter

For string data, use the typed getter:

kotlin
val username = intent.getStringExtra("extra_username")

This is better than using a more generic API and casting manually, because the intent already knows how to return the right nullable type.

A Cleaner Launch Pattern

To avoid repeating keys and intent-building logic everywhere, wrap the launch step in a helper:

kotlin
1import android.content.Context
2import android.content.Intent
3
4class DetailActivity : AppCompatActivity() {
5
6    companion object {
7        private const val EXTRA_USERNAME = "extra_username"
8
9        fun createIntent(context: Context, username: String): Intent {
10            return Intent(context, DetailActivity::class.java)
11                .putExtra(EXTRA_USERNAME, username)
12        }
13    }
14}

Then the sender uses:

kotlin
startActivity(DetailActivity.createIntent(this, "mark"))

This keeps the extra key private to the destination activity and reduces mistakes.

Retrieval from a Bundle

Intent extras are also accessible through the extras bundle:

kotlin
val username = intent.extras?.getString(MainActivity.EXTRA_USERNAME)

This works, but getStringExtra is often clearer when you only need one string directly from the intent.

Keep Extras Small and Intentional

String extras are great for IDs, names, titles, and small configuration flags. They are not a good place to move large object graphs or entire screens of data. If the receiving activity can load its own data from an ID, passing the ID is usually cleaner.

That keeps navigation messages simple and resilient.

Common Pitfalls

The biggest pitfall is mismatched keys. If the sender uses one string constant and the receiver uses another, the extra comes back as null.

Another common mistake is assuming getStringExtra can never return null. It absolutely can, either because the extra was absent or because the activity was launched differently.

People also scatter raw string keys throughout the app. Shared constants or intent-builder helpers make this much easier to maintain.

For larger apps, that small design choice prevents subtle bugs where one activity silently looks for a slightly different key than the sender used.

It also makes it easier to define sensible defaults in the receiver, because the contract for what may or may not be present is centralized in one place.

Summary

  • Use putExtra(key, value) to attach string data to an Intent.
  • Read it with getStringExtra(key) on the receiving side.
  • Keep the key consistent by using constants or helper methods.
  • Handle null safely because a string extra may be missing.
  • Prefer passing small values such as IDs or names instead of large payloads.
  • Centralized intent-builder helpers make navigation and testing easier to maintain.

Course illustration
Course illustration

All Rights Reserved.