// Step 1: Import necessary packages
import android.content.Intent
import android.os.Bundle
import androidx.activity.result.contract.ActivityResultContracts
import androidx.appcompat.app.AppCompatActivity
class YourActivity : AppCompatActivity() {
// Step 2: Declare an ActivityResultLauncher variable
private val someActivityResultLauncher =
registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
// Step 6: Handle the result in the callback
if (result.resultCode == RESULT_OK) {
// Handle the successful result
val data: Intent? = result.data
// Perform actions with the data if needed
} else {
// Handle the result when it's not OK
}
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
// Step 3: Create an Intent to launch another activity
val someIntent = Intent(this, AnotherActivity::class.java)
// Step 4: Set up any necessary data or extras for the intent
// Step 5: Launch the activity using the ActivityResultLauncher
someActivityResultLauncher.launch(someIntent)
}
}