OnActivityResult method is deprecated, you are looking for an alternative? Here It is the Latest Solution in android | Kotlin
One of the most used method OnActivityResult is deprecated in android. Here it is the alternative solution you can follow-
![]() |
In First Activity, take val resultLauncher using below code-
private val resultLauncher = registerForActivityResult(StartActivityForResult()) { result -> if (result.resultCode == Activity.RESULT_OK) { try { val data: Intent? = result.data
val stringResponse = data?.getStringExtra("response")!!
} catch (e: Exception) { e.printStackTrace() } } }
Now Call this val, where you want to make startActivityFor Result
override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) button.setOnClickListener { val intent = Intent(this, SecondActivity::class.java) resultLauncher.launch(intent) } }
In second Activity return the value-
private fun successResult(returnString: String) { val intent: Intent = intent intent.putExtra("response", returnString)
setResult(Activity.RESULT_OK, intent) finish() } private fun cancelResult() { val intent: Intent = intent setResult(Activity.RESULT_CANCELED, intent) finish() }
Thanks,
Hii
ReplyDeleteThis is a well-researched and informative piece. I appreciate the depth of information and the clarity of your writing. Great job!
ReplyDeleteBest Embedded Training in chennai
Best Embedded Training in coimbatore
Thank you so much for your kind words! I'm thrilled to hear that you found the piece well-researched and informative. Your feedback means a lot to me and motivates me to keep delivering quality content.
Delete