Hi all I'm new to android development(I'm using Kotlin) and startActivityForResult is deprecated trying to make one simple app To make it simple :
I have a main activity with two button first activity and second activity.
I have a "child" activity with a text fields and a finish button.
When I click the main activity button, the child activity opens (it works data can also be access which i pass from main activity). Then I put some text to the text fields and when I click the finish button I want the data from the text fields to be transferred to the main activity, and I can't get this working. followed medium, yt and stackoverflow not able to make it work
my main activity code
package com.example.data
import android.app.Activity
import android.app.Instrumentation
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.preference.PreferenceManager
import android.widget.Button
import android.widget.EditText
import android.widget.TextView
import android.widget.Toast
import androidx.activity.result.ActivityResult
import androidx.activity.result.ActivityResultCallback
import androidx.activity.result.ActivityResultLauncher
import androidx.activity.result.contract.ActivityResultContracts
class MainActivity : AppCompatActivity() {
companion object{
private const val FIRST_ACTIVITY_RESULT = 1
}
lateinit var activityResultLauncher: ActivityResultLauncher<Intent>
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val firstbtn = findViewById<Button>(R.id.first_activity_btn)
val first_text = findViewById<TextView>(R.id.first_activity_text)
val secondbtn = findViewById<Button>(R.id.second_activity_btn)
val edit = findViewById<EditText>(R.id.edit_name)
firstbtn.setOnClickListener{
var intent = Intent(this,first_activity::class.java)
intent.putExtra("data_name",edit.text.toString())
activityResultLauncher.launch(intent)
finish()
}
secondbtn.setOnClickListener{
var intent = Intent(this,second_activity::class.java)
startActivity(intent)
finish()
}
activityResultLauncher = registerForActivityResult(
ActivityResultContracts.StartActivityForResult()){ result:ActivityResult?->
if (result!!.resultCode == Activity.RESULT_OK){
if(result.data!!.extras!!.getString("data_transfer").toString() == "yes"){
Toast.makeText(applicationContext, "user send reply", Toast.LENGTH_SHORT).show()
}else{
Toast.makeText(applicationContext, "sorry user did not send reply", Toast.LENGTH_SHORT).show()
}
}
}
}
}
first activity code
package com.example.data
import android.app.Activity
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import android.widget.EditText
import android.widget.TextView
class first_activity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_first)
val finish = findViewById<Button>(R.id.finish_btn)
val textData = findViewById<TextView>(R.id.tv_first_activity)
val edit = findViewById<EditText>(R.id.edit_text_first_activity)
textData.text = intent.extras!!.getString("data_name")
finish.setOnClickListener{
// val intent = Intent(this,MainActivity::class.java)
// intent.putExtra("success","success message woohooo!!!")
// startActivity(intent)
val intent = Intent()
// val intent = Intent(this,MainActivity::class.java)
intent.putExtra("data_transfer",edit.text.toString())
setResult(Activity.RESULT_OK,intent)
// startActivity(intent)
finish()
}
}
}