Skip to content

Instantly share code, notes, and snippets.

@davidair
Created November 9, 2018 23:12
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save davidair/1b13a21c1cf8c7e226411ffc663b8590 to your computer and use it in GitHub Desktop.
Save davidair/1b13a21c1cf8c7e226411ffc663b8590 to your computer and use it in GitHub Desktop.
A base class activity that leverages Kotlin coroutines and allows streamlining activity invocation
/**
* Copyright 2017 Google LLC.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
import android.content.Intent
import android.support.v7.app.AppCompatActivity
import kotlinx.coroutines.CompletableDeferred
import kotlinx.coroutines.Deferred;
/**
* Wraps the parameters of onActivityResult
*
* @property resultCode the result code returned from the activity.
* @property data the optional intent returned from the activity.
*/
class ActivityResult(
val resultCode: Int,
val data: Intent?) {
}
/**
* Provides a mechanism to launch intents using co-routines.
* Example usage:
*
* GlobalScope.launch(Dispatchers.Main) {
* val result = launchIntent(intent).await()
* result?.data?.let {
* // Do something with "it"
* }
* }
*/
abstract class AsyncActivity : AppCompatActivity() {
var currentCode : Int = 0
var resultByCode = mutableMapOf<Int, CompletableDeferred<ActivityResult?>>()
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
resultByCode[requestCode]?.let {
it.complete(ActivityResult(resultCode, data))
resultByCode.remove(requestCode)
} ?: run {
super.onActivityResult(requestCode, resultCode, data)
}
}
/**
* Launches the intent allowing to process the result using await()
*
* @param intent the intent to be launched.
*
* @return Deferred<ActivityResult>
*/
fun launchIntent(intent: Intent) : Deferred<ActivityResult?>
{
val activityResult = CompletableDeferred<ActivityResult?>()
if (intent.resolveActivity(packageManager) != null) {
val resultCode = currentCode++
resultByCode[resultCode] = activityResult
startActivityForResult(intent, resultCode)
} else {
activityResult.complete(null)
}
return activityResult
}
}
@nachtien
Copy link

nachtien commented Dec 11, 2018

This is so great. But how do you handle the case when the calling activity (the Activity who calls startActivityForResult) is destroyed?

@integrer
Copy link

Such a great idea to using kotlin's Deffered objects to work with activity results, but there is some mistakes:

  1. Fields, introduced here, that represents internal object state, exposed (implicitly) as public. Also this can be defined as val because never reassigned.
  2. Variable, that introduced here as resultCode, in next used here as requestCode.
  3. Method onActivityResult can be simplified:
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
    resultByCode.remove(requestCode)?.complete(ActivityResult(resultCode, data))
        ?: super.onActivityResult(requestCode, resultCode, data)
}

@mykola-dev
Copy link

Unfortunately it never survives process killing. e.g. when start camera app and your app become background.

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