Skip to content

Instantly share code, notes, and snippets.

@ValeraSetrakov
Created September 9, 2019 13:24
Show Gist options
  • Save ValeraSetrakov/1812a8f2b2c458a9868f5b95506bb888 to your computer and use it in GitHub Desktop.
Save ValeraSetrakov/1812a8f2b2c458a9868f5b95506bb888 to your computer and use it in GitHub Desktop.
class ActivityA: AppCompatActivity() {
companion object {
private const val SOME_CODE = 1
}
private fun someAction() {
ActivityB.startForResult(this, "some param", SOME_CODE)
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (requestCode == SOME_CODE) {
if(resultCode == Activity.RESULT_OK) {
//to do something
}
}
}
}
class ActivityB: AppCompatActivity() {
companion object {
private const val PARAM_KEY = "PARAM_KEY"
fun startForResult(activity: Activity, param: String, code: Int) {
val intent = Intent(activity, ActivityB::class.java).apply {
putExtra(PARAM_KEY, param)
}
activity.startActivityForResult(intent, code)
}
}
private var param = ""
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
intent?.apply {
param = getStringExtra(PARAM_KEY)
}
}
fun someAction() {
setResult(Activity.RESULT_OK)
finish()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment