From a429a5e0dbc96ae67bc68e27527239a43390e3f1 Mon Sep 17 00:00:00 2001 From: Allan Wang Date: Mon, 12 Jun 2017 16:24:29 -0700 Subject: Add fragment bundler --- .../main/kotlin/ca/allanwang/kau/utils/ContextUtils.kt | 5 +++-- .../main/kotlin/ca/allanwang/kau/utils/FragmentUtils.kt | 17 +++-------------- 2 files changed, 6 insertions(+), 16 deletions(-) (limited to 'library/src/main/kotlin') 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 a856ed2..e2c1eb8 100644 --- a/library/src/main/kotlin/ca/allanwang/kau/utils/ContextUtils.kt +++ b/library/src/main/kotlin/ca/allanwang/kau/utils/ContextUtils.kt @@ -6,6 +6,7 @@ import android.content.Intent import android.graphics.Color import android.graphics.drawable.Drawable import android.net.ConnectivityManager +import android.os.Bundle import android.os.Handler import android.support.annotation.* import android.support.v4.content.ContextCompat @@ -30,11 +31,11 @@ fun Activity.restart(extras: ((Intent) -> Unit)? = null) { overridePendingTransition(0, 0) } -fun Context.startActivity(clazz: Class, clearStack: Boolean = false, intentBuilder: Intent.() -> Unit = {}) { +fun Context.startActivity(clazz: Class, clearStack: Boolean = false, intentBuilder: Intent.() -> Unit = {}, bundle: Bundle? = null) { 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) + ContextCompat.startActivity(this, intent, bundle) if (this is Activity && clearStack) finish() } diff --git a/library/src/main/kotlin/ca/allanwang/kau/utils/FragmentUtils.kt b/library/src/main/kotlin/ca/allanwang/kau/utils/FragmentUtils.kt index 2ef1232..f97b4d3 100644 --- a/library/src/main/kotlin/ca/allanwang/kau/utils/FragmentUtils.kt +++ b/library/src/main/kotlin/ca/allanwang/kau/utils/FragmentUtils.kt @@ -6,19 +6,8 @@ import android.support.v4.app.Fragment /** * Created by Allan Wang on 2017-05-29. */ - -private fun Fragment.bundle(): Bundle { - if (this.arguments == null) - this.arguments = Bundle() - return this.arguments -} - -fun T.putString(key: String, value: String): T { - this.bundle().putString(key, value) - return this -} - -fun T.putInt(key: String, value: Int): T { - this.bundle().putInt(key, value) +fun T.withBundle(builder: Bundle.() -> Unit = {}): T { + if (this.arguments == null) this.arguments = Bundle() + this.arguments.builder() return this } \ No newline at end of file -- cgit v1.2.3