0

Firstly, I have a function with result callback that starts another activity:

object Functions {
  fun myFunction(context: Context, onResult: (resultCode: Int) -> Unit) {
    context.startActivity(Intent(context, DelegateActivity::class.java))
  }
}

Secondly, I have DelegateActivity (that receives a result from ResultActivity) and ResultActivity:

class DelegateActivity : Activity() {
  override fun onCreate(savedInstanceState: Bundle?) {
    startActivityForResult(Intent(this, ResultActivity::class.java))
  }
  
  override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
     super.onActivityResult(requestCode, resultCode, data)
     // TODO: send result to onResult(resultCode) callback!
  }
}

class ResultActivity : Activity() {
  override fun onCreate(savedInstanceState: Bundle?) {
    // some code with setResult()
  }  
}

My problem is that I have to forward the result from DelegateActivity to the onResult() - It's a requirement. How can I do this?

I can use EventBus/RxBus, but is there a better way?

  • refer https://stackoverflow.com/questions/21780252/how-to-use-onactivityresult-method-from-other-than-activity-class – sasikumar Jan 25 '22 at 12:26
  • Can you explain why you can't use `onActivityResult` directly in the Activity/Fragment that you are using to call this `Functions.myFunction`? If you try to do what you're describing, you will leak your Activity and it won't work if there is a configuration change during the time the second activity is open. – Tenfour04 Jan 25 '22 at 14:14
  • @Tenfour04, it is a library requirement. – Artur Ilkaev Jan 26 '22 at 15:03
  • Can you share which library it is? I think either you have a misunderstanding about how to use the library, or the library has a flawed design that will leak activities. – Tenfour04 Jan 26 '22 at 15:10
  • So its my library. There will be no leaks with EventBus, I realized it, but I am finding better solution. – Artur Ilkaev Jan 26 '22 at 17:33

0 Answers0