Last active
July 8, 2017 14:35
-
-
Save meoyawn/a72ea3ab3e99ffce3dfd34ae1c2275bf to your computer and use it in GitHub Desktop.
This file contains hidden or 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
import android.app.Activity | |
import android.content.Intent | |
import android.os.Bundle | |
import kotlinx.coroutines.experimental.suspendCancellableCoroutine | |
typealias RequestCode = Int | |
typealias ResultCode = Int | |
data class ActivityResult( | |
val requestCode: RequestCode, | |
val resultCode: ResultCode, | |
val data: Intent? | |
) | |
object MutableRelay { | |
var request: Intent? = null | |
var result: ((ActivityResult) -> Unit)? = null | |
} | |
val REQUEST_CODE = 1337 | |
class ResultActivity : Activity() { | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
MutableRelay.request | |
?.let { startActivityForResult(it, REQUEST_CODE) } | |
?: finish() | |
} | |
override fun onActivityResult(requestCode: RequestCode, resultCode: ResultCode, data: Intent?) { | |
super.onActivityResult(requestCode, resultCode, data) | |
finish() | |
if (requestCode == REQUEST_CODE) { | |
MutableRelay.result?.let { | |
it(ActivityResult(requestCode = requestCode, | |
resultCode = resultCode, | |
data = data)) | |
} | |
} | |
} | |
} | |
suspend fun Activity.awaitActivityForResult(intent: Intent): ActivityResult = | |
suspendCancellableCoroutine { cont -> | |
cont.invokeOnCompletion { | |
MutableRelay.request = null | |
MutableRelay.result = null | |
} | |
MutableRelay.request = intent | |
MutableRelay.result = { cont.resume(it) } | |
startActivity(Intent(this, ResultActivity::class.java)) | |
} |
This file contains hidden or 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
<style name="Theme.Transparent" parent="android:Theme"> | |
<item name="android:windowIsTranslucent">true</item> | |
<item name="android:windowBackground">@android:color/transparent</item> | |
<item name="android:windowContentOverlay">@null</item> | |
<item name="android:windowNoTitle">true</item> | |
<item name="android:windowIsFloating">true</item> | |
<item name="android:backgroundDimEnabled">false</item> | |
</style> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment