0
0
Android Kotlinmobile~3 mins

Why Activity results in Android Kotlin? - Purpose & Use Cases

Choose your learning style9 modes available
The Big Idea

What if you could get data back from another screen without messy code or lost info?

The Scenario

Imagine you have two screens in your app: one to pick a photo and another to show it. You want to send the photo back from the picker screen to the main screen manually.

You try to pass data by saving it somewhere temporary or using complicated global variables.

The Problem

This manual way is slow and confusing. You might lose data if the app closes or crashes. It's hard to keep track of where the data is and when it arrives. Bugs happen easily, and the code becomes messy.

The Solution

Activity results let you start a screen and wait for it to send back data cleanly. Android handles the communication for you. You just write simple code to get the result when the other screen finishes.

Before vs After
Before
startActivity(intent)
// no easy way to get result back
After
val launcher = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
  if (result.resultCode == RESULT_OK) {
    val data = result.data
    // use returned data
  }
}
launcher.launch(intent)
What It Enables

You can easily get data back from another screen, making your app interactive and user-friendly without messy code.

Real Life Example

When a user picks a contact from their address book, your app can get the chosen contact's info right away and show it on the main screen.

Key Takeaways

Manual data passing between screens is error-prone and complicated.

Activity results provide a clean, reliable way to get data back from another screen.

This makes your app easier to build, understand, and maintain.