From dcdf008c1e83af4ba0509736a389522a71797264 Mon Sep 17 00:00:00 2001 From: Allan Wang Date: Mon, 12 Jun 2017 16:15:00 -0700 Subject: Add start activity --- README.md | 1 + library/src/main/kotlin/ca/allanwang/kau/utils/ContextUtils.kt | 8 ++++++++ library/src/main/res/layout/kau_activity_kpref.xml | 5 +++-- 3 files changed, 12 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 30cfe67..17145d5 100644 --- a/README.md +++ b/README.md @@ -143,6 +143,7 @@ The canvas also supports color fading and direct color setting so it can effecti > Extends Activity * Restart an activity > Extends Context +* Start Activity using the class, with optional intents and stack clearing * Create a toast directly * Get resource values through `.color(id)`, `.dimen(id)`, `.drawable(id)`, `.integer(id)`, `.string(id)` * Get attribute values through resolve methods diff --git a/library/src/main/kotlin/ca/allanwang/kau/utils/ContextUtils.kt b/library/src/main/kotlin/ca/allanwang/kau/utils/ContextUtils.kt index 5547fe1..a856ed2 100644 --- a/library/src/main/kotlin/ca/allanwang/kau/utils/ContextUtils.kt +++ b/library/src/main/kotlin/ca/allanwang/kau/utils/ContextUtils.kt @@ -30,6 +30,14 @@ fun Activity.restart(extras: ((Intent) -> Unit)? = null) { overridePendingTransition(0, 0) } +fun Context.startActivity(clazz: Class, clearStack: Boolean = false, intentBuilder: Intent.() -> Unit = {}) { + val intent = (Intent(this, clazz)) + if (clearStack) intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_SINGLE_TOP or Intent.FLAG_ACTIVITY_NEW_TASK) + intent.intentBuilder() + startActivity(intent) + if (this is Activity && clearStack) finish() +} + var Activity.navigationBarColor: Int get() = if (buildIsLollipopAndUp) window.navigationBarColor else Color.BLACK set(value) { diff --git a/library/src/main/res/layout/kau_activity_kpref.xml b/library/src/main/res/layout/kau_activity_kpref.xml index 35f16cd..4135edc 100644 --- a/library/src/main/res/layout/kau_activity_kpref.xml +++ b/library/src/main/res/layout/kau_activity_kpref.xml @@ -8,16 +8,17 @@ android:id="@+id/kau_toolbar_ripple" android:layout_width="0dp" android:layout_height="0dp" - app:layout_constraintTop_toTopOf="parent" + app:layout_constraintBottom_toBottomOf="@+id/kau_toolbar" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" - app:layout_constraintBottom_toBottomOf="@+id/kau_toolbar" /> + app:layout_constraintTop_toTopOf="parent" /> -- cgit v1.2.3