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:
First, create a new Android project in Android Studio or open an existing project.
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
.In the
MainActivity
class, import the necessary classes:
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
- Make the
MainActivity
class extendAppCompatActivity
:
class MainActivity : AppCompatActivity() {
- Inside the
MainActivity
class, create a constant to hold the request code for the activity result:
companion object {
const val REQUEST_CODE = 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)
}
- 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" />
- Back in the
MainActivity
class, create a function calledstartActivityForResult
that will handle the button click event:
fun startActivityForResult(view: View) {
val intent = Intent(this, SecondActivity::class.java)
startActivityForResult(intent, REQUEST_CODE)
}
Create a new Kotlin class for the second activity. For example, let's call it
SecondActivity
.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
- Make the
SecondActivity
class extendAppCompatActivity
:
class SecondActivity : AppCompatActivity() {
- 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)
}
- In the
SecondActivity
class, create a button in theactivity_second.xml
layout file that will return a result to theMainActivity
. For example:
<Button
android:id="@+id/returnButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Return Result"
android:onClick="returnResult" />
- Back in the
SecondActivity
class, create a function calledreturnResult
that will handle the button click event and send the result back to theMainActivity
:
fun returnResult(view: View) {
val intent = Intent()
intent.putExtra("result", "Hello from SecondActivity!")
setResult(Activity.RESULT_OK, intent)
finish()
}
- Finally, go back to the
MainActivity
class and override theonActivityResult
method to handle the result returned from theSecondActivity
:
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
.