Free tutorials & notes in Hindi & English · Clean code examples 🧰 Free PDF & Image Tools →
📋 Screens, Lists & Navigation  ·  Lesson 21

Pass Data Between Activities

Send simple values with extras

An Intent can carry key–value pairs called extras: String, Int, Boolean, Double, arrays and Parcelable objects.

Kotlin – sender
// MainActivity.kt
binding.btnOpen.setOnClickListener {
    val intent = Intent(this, ProfileActivity::class.java).apply {
        putExtra("name", "Riya Sharma")
        putExtra("marks", 92)
        putExtra("isTopper", true)
    }
    startActivity(intent)
}

Read them in the second screen

Kotlin – receiver
// ProfileActivity.kt
override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    binding = ActivityProfileBinding.inflate(layoutInflater)
    setContentView(binding.root)

    val name = intent.getStringExtra("name") ?: "Unknown"     // String? → default
    val marks = intent.getIntExtra("marks", 0)               // default if missing
    val isTopper = intent.getBooleanExtra("isTopper", false)

    binding.tvName.text = name
    binding.tvMarks.text = "Marks: $marks"
    binding.tvBadge.isVisible = isTopper
}
9:41▮▮ 85%Students⋮RRiya Sharma92 marksAAman Verma88 marksSSara Khan81 marks9:41▮▮ 85%←Profile⋮Riya SharmaMarks: 92🏆 Topper
Tap a student → ProfileActivity reads the extras

Keep keys in one place

Kotlin – companion object factory
class ProfileActivity : AppCompatActivity() {
    companion object {
        const val EXTRA_NAME = "extra_name"
        const val EXTRA_MARKS = "extra_marks"

        // a "factory" – callers cannot forget a key
        fun newIntent(context: Context, name: String, marks: Int) =
            Intent(context, ProfileActivity::class.java)
                .putExtra(EXTRA_NAME, name)
                .putExtra(EXTRA_MARKS, marks)
    }
}

// caller
startActivity(ProfileActivity.newIntent(this, "Riya Sharma", 92))

Send a whole object (@Parcelize)

Kotlin DSL – enable the plugin
// app/build.gradle.kts
plugins {
    alias(libs.plugins.android.application)
    alias(libs.plugins.kotlin.android)
    id("kotlin-parcelize")                 // add this line
}
Kotlin
import android.os.Parcelable
import kotlinx.parcelize.Parcelize

@Parcelize
data class Student(val id: Int, val name: String, val marks: Int, val city: String) : Parcelable

// send
val student = Student(7, "Aman Verma", 88, "Aligarh")
startActivity(Intent(this, ProfileActivity::class.java).putExtra("student", student))

// receive (API 33+ has a type-safe version; IntentCompat works on all versions)
val s: Student? = IntentCompat.getParcelableExtra(intent, "student", Student::class.java)
binding.tvName.text = s?.name

Get a result back

Kotlin – Activity Result API
// MainActivity – open the editor and wait for the new name
private val editName = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
    if (result.resultCode == RESULT_OK) {
        val newName = result.data?.getStringExtra("new_name")
        binding.tvName.text = newName
    }
}

binding.btnEdit.setOnClickListener {
    editName.launch(Intent(this, EditNameActivity::class.java).putExtra("old_name", binding.tvName.text.toString()))
}

// EditNameActivity – send the result back
binding.etName.setText(intent.getStringExtra("old_name"))
binding.btnSave.setOnClickListener {
    val data = Intent().putExtra("new_name", binding.etName.text.toString())
    setResult(RESULT_OK, data)
    finish()
}

The full round trip

MainActivityEditNameActivitylaunch(intent + old_name)user edits the namesetResult(RESULT_OK, new_name) + finish()callback: update tvName
Send data forward, get a result back

Common mistakes

  • Key typo ("Name" vs "name") → value is null. Use constants.
  • Reading extras with the wrong type (getStringExtra for an Int) → null/default.
  • Calling registerForActivityResult inside a click listener → crash; register it as a property (before the Activity is started).
  • Passing huge lists/bitmaps → TransactionTooLargeException.

Summary

  • putExtra(key, value) → getXxxExtra(key, default).
  • Objects: @Parcelize data class … : Parcelable.
  • Results: registerForActivityResult + setResult + finish().
✅ Practice

Create a list of 3 students. Tapping one opens ProfileActivity with a Parcelable Student. Add an "Edit marks" button that returns the new marks to the list screen.

Frequently Asked Questions

Is startActivityForResult still used?
It is deprecated. Use registerForActivityResult with ActivityResultContracts.StartActivityForResult (or a specific contract). It is lifecycle-safe and easier to test.
Can I pass a large list through an Intent?
No. The Intent bundle has a size limit (about 1 MB shared). Pass an id and load the data from a database or a shared ViewModel in the next screen.
Parcelable or Serializable?
Parcelable is faster on Android and, with the kotlin-parcelize plugin, needs only one annotation. Prefer it.
← Android Tutorial (Kotlin)
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

Next topicRecyclerView (Lists) Your progress: 21 / 43 (49%) · Lesson 22 / 43 Continue →

How was this lesson?

Your feedback helps us make the content better.

Write a review or suggestion (optional)

After approval, your name and city may appear publicly with your review. Please do not write your email or phone number.

▶ CodingEasily on YouTube

Learn with video lessons in easy Hindi

Featured video: RDBMS Part 3 – Class XI Commerce IT

▶ Subscribe – it’s free