SHARE
TWEET

Untitled

a guest Sep 20th, 2019 100 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class ActivityA: AppCompatActivity() {
  2.    
  3.     companion object {
  4.         private const val SOME_CODE = 1
  5.     }
  6.    
  7.     private fun someAction() {
  8.         ActivityB.startForResult(this, "some param", SOME_CODE)
  9.     }
  10.  
  11.     override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
  12.         super.onActivityResult(requestCode, resultCode, data)
  13.         if (requestCode == SOME_CODE) {
  14.             if(resultCode == Activity.RESULT_OK) {
  15.                 //to do something
  16.             }
  17.         }
  18.     }
  19. }
  20. class ActivityB: AppCompatActivity() {
  21.  
  22.     companion object {
  23.         private const val PARAM_KEY = "PARAM_KEY"
  24.         fun startForResult(activity: Activity, param: String, code: Int) {
  25.             val intent = Intent(activity, ActivityB::class.java).apply {
  26.                 putExtra(PARAM_KEY, param)
  27.             }
  28.             activity.startActivityForResult(intent, code)
  29.         }
  30.     }
  31.    
  32.     private var param = ""
  33.  
  34.     override fun onCreate(savedInstanceState: Bundle?) {
  35.         super.onCreate(savedInstanceState)
  36.         intent?.apply {
  37.             param = getStringExtra(PARAM_KEY)
  38.         }
  39.     }
  40.    
  41.     fun someAction() {
  42.         setResult(Activity.RESULT_OK)
  43.         finish()
  44.     }
  45.    
  46. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top