diff options
Diffstat (limited to 'core/src/main/kotlin')
-rw-r--r-- | core/src/main/kotlin/ca/allanwang/kau/utils/ActivityUtils.kt | 25 | ||||
-rw-r--r-- | core/src/main/kotlin/ca/allanwang/kau/utils/ContextUtils.kt | 10 |
2 files changed, 31 insertions, 4 deletions
diff --git a/core/src/main/kotlin/ca/allanwang/kau/utils/ActivityUtils.kt b/core/src/main/kotlin/ca/allanwang/kau/utils/ActivityUtils.kt index 54aeaff..5631e70 100644 --- a/core/src/main/kotlin/ca/allanwang/kau/utils/ActivityUtils.kt +++ b/core/src/main/kotlin/ca/allanwang/kau/utils/ActivityUtils.kt @@ -2,11 +2,11 @@ package ca.allanwang.kau.utils import android.annotation.SuppressLint import android.app.Activity +import android.app.ActivityOptions import android.content.Intent import android.graphics.Color -import android.os.Build +import android.os.Bundle import android.support.annotation.ColorInt -import android.support.annotation.RequiresApi import android.support.annotation.StringRes import android.support.design.widget.Snackbar import android.view.Menu @@ -20,6 +20,27 @@ import org.jetbrains.anko.contentView */ /** + * Helper class to launch an activity for result + * Counterpart of [Activity.startActivityForResult] + * For starting activities without result, see [startActivity] + */ +@SuppressLint("NewApi") +fun Activity.startActivityForResult( + clazz: Class<out Activity>, + requestCode: Int, + transition: Boolean = false, + bundle: Bundle? = null, + intentBuilder: Intent.() -> Unit = {}) { + val intent = Intent(this, clazz) + val fullBundle = if (transition && buildIsLollipopAndUp) + ActivityOptions.makeSceneTransitionAnimation(this).toBundle() + else Bundle() + if (bundle != null) fullBundle.putAll(bundle) + intent.intentBuilder() + startActivityForResult(intent, requestCode, if (fullBundle.isEmpty) null else fullBundle) +} + +/** * Restarts an activity from itself with a fade animation * Keeps its existing extra bundles and has a builder to accept other parameters */ diff --git a/core/src/main/kotlin/ca/allanwang/kau/utils/ContextUtils.kt b/core/src/main/kotlin/ca/allanwang/kau/utils/ContextUtils.kt index 7a92665..2219b5d 100644 --- a/core/src/main/kotlin/ca/allanwang/kau/utils/ContextUtils.kt +++ b/core/src/main/kotlin/ca/allanwang/kau/utils/ContextUtils.kt @@ -26,6 +26,12 @@ import com.afollestad.materialdialogs.MaterialDialog /** * Created by Allan Wang on 2017-06-03. */ + +/** + * Helper class to launch an activity from a context + * Counterpart of [ContextCompat.startActivity] + * For starting activities for results, see [startActivityForResult] + */ @SuppressLint("NewApi") fun Context.startActivity( clazz: Class<out Activity>, @@ -33,14 +39,14 @@ fun Context.startActivity( transition: Boolean = false, bundle: Bundle? = null, intentBuilder: Intent.() -> Unit = {}) { - val intent = (Intent(this, clazz)) + val intent = Intent(this, clazz) if (clearStack) intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK or Intent.FLAG_ACTIVITY_NEW_TASK) - intent.intentBuilder() val fullBundle = if (transition && this is Activity && buildIsLollipopAndUp) ActivityOptions.makeSceneTransitionAnimation(this).toBundle() else Bundle() if (transition && this !is Activity) KL.d("Cannot make scene transition when context is not an instance of an Activity") if (bundle != null) fullBundle.putAll(bundle) + intent.intentBuilder() ContextCompat.startActivity(this, intent, if (fullBundle.isEmpty) null else fullBundle) if (this is Activity && clearStack) finish() } |