Created
September 9, 2019 13:24
-
-
Save ValeraSetrakov/1812a8f2b2c458a9868f5b95506bb888 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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