Skip to content

Instantly share code, notes, and snippets.

@cbeyls
Forked from yanngx/FragmentArgumentDelegate.kt
Last active October 16, 2018 13:17
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cbeyls/90d4492e483d075f369a658cdfecf582 to your computer and use it in GitHub Desktop.
Save cbeyls/90d4492e483d075f369a658cdfecf582 to your computer and use it in GitHub Desktop.
Fragment arguments without hassle !
package be.brol
import android.os.Binder
import android.os.Bundle
import android.os.Parcelable
import android.support.v4.app.BundleCompat
import android.support.v4.app.Fragment
import kotlin.reflect.KProperty
/**
* Eases the Fragment.newInstance ceremony by marking the fragment's args with this delegate
* Just write the property in newInstance and read it like any other property after the fragment has been created
*
* Inspired by Jake Wharton, he mentioned it during his IO/17 talk about Kotlin
*/
object FragmentArgumentDelegate {
operator fun <T : Any> getValue(thisRef: Fragment, property: KProperty<*>): T {
val args = thisRef.arguments ?: throw IllegalStateException("Cannot read property ${property.name} if no arguments have been set")
@Suppress("UNCHECKED_CAST")
return args.get(property.name) as T? ?: throw IllegalStateException("Property ${property.name} could not be read")
}
operator fun <T : Any> setValue(thisRef: Fragment, property: KProperty<*>, value: T) {
var args = thisRef.arguments
if (args == null) {
args = Bundle()
thisRef.arguments = args
}
val key = property.name
when (value) {
is String -> args.putString(key, value)
is ByteArray -> args.putByteArray(key, value)
is CharArray -> args.putCharArray(key, value)
is CharSequence -> args.putCharSequence(key, value)
is Bundle -> args.putBundle(key, value)
is Binder -> BundleCompat.putBinder(args, key, value)
is Parcelable -> args.putParcelable(key, value)
is java.io.Serializable -> args.putSerializable(key, value)
else -> throw IllegalStateException("Type ${value.javaClass.canonicalName} of property ${property.name} is not supported")
}
}
}
package be.brol
import android.os.Bundle
import android.support.v4.app.Fragment
import android.widget.Toast
/**
* Example usage of FragmentArgumentDelegate
*/
class WeatherCityFragment : Fragment() {
private var cityId: String by FragmentArgumentDelegate
override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
Toast.makeText(activity, cityId, Toast.LENGTH_SHORT).show()
}
companion object {
fun newInstance(cityId: String) = WeatherCityFragment().apply {
this.cityId = cityId
}
}
}
@cbeyls
Copy link
Author

cbeyls commented Jun 4, 2017

I added the following changes compared to the original version:

  • The delegate is a singleton object, so each fragment argument will be backed by the same instance. The argument will not be cached but read directly from the HashMap/ArrayMap inside the Bundle which should be fast enough.
  • To write primitive types like int or long, putSerializable() is used. Because calling the generic setValue() method will already box the primitive value to an object which implements Serializable. Then we can avoid unboxing that object and reboxing it again because the value ends up being stored as an object in a HashMap/ArrayMap inside the bundle. So single boxing instead of double boxing and shorter code.

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