Skip to main content

How to start an activity for result in Kotlin Android

How to start an activity for result in Kotlin Android.

Here is a step-by-step tutorial on how to start an activity for result in Kotlin Android:

  1. First, create a new Android project in Android Studio or open an existing project.

  2. Create a new Kotlin class for the activity from where you want to start another activity for result. For example, let's call it MainActivity.

  3. In the MainActivity class, import the necessary classes:

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
  1. Make the MainActivity class extend AppCompatActivity:
class MainActivity : AppCompatActivity() {
  1. Inside the MainActivity class, create a constant to hold the request code for the activity result:
companion object {
const val REQUEST_CODE = 1
}
  1. Override the onCreate method and set the content view for the activity:
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
  1. Create a button in the activity_main.xml layout file that will trigger the start of the second activity. For example:
<Button
android:id="@+id/startButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Start Activity"
android:onClick="startActivityForResult" />
  1. Back in the MainActivity class, create a function called startActivityForResult that will handle the button click event:
fun startActivityForResult(view: View) {
val intent = Intent(this, SecondActivity::class.java)
startActivityForResult(intent, REQUEST_CODE)
}
  1. Create a new Kotlin class for the second activity. For example, let's call it SecondActivity.

  2. In the SecondActivity class, import the necessary classes:

import android.app.Activity
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
  1. Make the SecondActivity class extend AppCompatActivity:
class SecondActivity : AppCompatActivity() {
  1. Override the onCreate method and set the content view for the activity:
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_second)
}
  1. In the SecondActivity class, create a button in the activity_second.xml layout file that will return a result to the MainActivity. For example:
<Button
android:id="@+id/returnButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Return Result"
android:onClick="returnResult" />
  1. Back in the SecondActivity class, create a function called returnResult that will handle the button click event and send the result back to the MainActivity:
fun returnResult(view: View) {
val intent = Intent()
intent.putExtra("result", "Hello from SecondActivity!")
setResult(Activity.RESULT_OK, intent)
finish()
}
  1. Finally, go back to the MainActivity class and override the onActivityResult method to handle the result returned from the SecondActivity:
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)

if (requestCode == REQUEST_CODE && resultCode == Activity.RESULT_OK) {
val result = data?.getStringExtra("result")
// Do something with the result
}
}

That's it! Now you can start the SecondActivity from the MainActivity, and when the SecondActivity returns a result, you can handle it in the onActivityResult method of the MainActivity.