Skip to content

Instantly share code, notes, and snippets.

@Dhaval2404
Created May 1, 2020 14:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Dhaval2404/7e34fb4bf34cb46f521d6a72f00e637e to your computer and use it in GitHub Desktop.
Save Dhaval2404/7e34fb4bf34cb46f521d6a72f00e637e to your computer and use it in GitHub Desktop.
Inline Activity Result
package com.github.dhaval2404.imagepicker.util
import android.content.Intent
import androidx.activity.result.ActivityResult
import androidx.activity.result.contract.ActivityResultContracts.StartActivityForResult
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.Fragment
/**
* Inline Activity Result
*
* @author Dhaval Patel
* @version 2.0
* @since 29 April 2020
*/
fun AppCompatActivity.startActivityForResult(
intent: Intent,
listener: (Int, Intent?) -> Unit?
) {
prepareCall(StartActivityForResult()) { result: ActivityResult ->
listener.invoke(result.resultCode, result.data)
}.launch(intent)
}
fun Fragment.startActivityForResult(intent: Intent, listener: (Int, Intent?) -> Unit?) {
prepareCall(StartActivityForResult()) { result: ActivityResult ->
listener.invoke(result.resultCode, result.data)
}.launch(intent)
}
@Dhaval2404
Copy link
Author

Required Gradle:

implementation 'androidx.activity:activity:1.2.0-alpha03'
implementation 'androidx.fragment:fragment:1.3.0-alpha03'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment