📋 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
}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?.nameGet 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
Common mistakes
- Key typo (
"Name"vs"name") → value is null. Use constants. - Reading extras with the wrong type (
getStringExtrafor an Int) → null/default. - Calling
registerForActivityResultinside 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.
