0
0
Android Kotlinmobile~5 mins

Activity results in Android Kotlin

Choose your learning style9 modes available
Introduction

Activity results let one screen send data back to the screen that started it. This helps apps share information between screens easily.

When you open a screen to pick a photo and want the chosen photo returned.
When you ask the user to fill a form on a new screen and need the filled data back.
When you start a screen to get user confirmation and want to know their choice.
When you want to get a result from a settings screen before continuing.
Syntax
Android Kotlin
val getResult = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
    if (result.resultCode == Activity.RESULT_OK) {
        val data = result.data
        // Use the returned data here
    }
}

// To start the activity
val intent = Intent(this, OtherActivity::class.java)
getResult.launch(intent)

This uses registerForActivityResult to handle results in a modern way.

Always check resultCode to know if the result is OK or cancelled.

Examples
Starts InputActivity and expects a string extra named "text" back.
Android Kotlin
val getResult = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
    if (result.resultCode == Activity.RESULT_OK) {
        val returnedText = result.data?.getStringExtra("text")
        // Use returnedText
    }
}

val intent = Intent(this, InputActivity::class.java)
getResult.launch(intent)
Launches a system picker to select an image and returns its Uri.
Android Kotlin
val getImage = registerForActivityResult(ActivityResultContracts.GetContent()) { uri ->
    if (uri != null) {
        // Use the image Uri
    }
}

getImage.launch("image/*")
Sample App

This app has two screens. The first screen has a button that opens the second screen. The second screen has a button that sends back a message to the first screen. The first screen shows the message in a TextView.

Android Kotlin
class MainActivity : AppCompatActivity() {
    private lateinit var resultLauncher: ActivityResultLauncher<Intent>

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        resultLauncher = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
            if (result.resultCode == Activity.RESULT_OK) {
                val returnedData = result.data?.getStringExtra("message")
                findViewById<TextView>(R.id.textView).text = returnedData ?: "No message"
            }
        }

        findViewById<Button>(R.id.button).setOnClickListener {
            val intent = Intent(this, SecondActivity::class.java)
            resultLauncher.launch(intent)
        }
    }
}

class SecondActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_second)

        findViewById<Button>(R.id.returnButton).setOnClickListener {
            val resultIntent = Intent().apply {
                putExtra("message", "Hello from SecondActivity")
            }
            setResult(Activity.RESULT_OK, resultIntent)
            finish()
        }
    }
}
OutputSuccess
Important Notes

Use registerForActivityResult instead of the old startActivityForResult method.

Always check if the returned data is null before using it to avoid crashes.

Remember to call setResult and finish() in the second activity to send data back.

Summary

Activity results let one screen send data back to the screen that started it.

Use registerForActivityResult to handle results safely and simply.

Always check the result code and returned data before using it.