Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Intent.putExtras()←これ

kobito-kaba
October 27, 2017

 Intent.putExtras()←これ

Activityを呼び出す儀式をめぐる冒険

kobito-kaba

October 27, 2017
Tweet

More Decks by kobito-kaba

Other Decks in Technology

Transcript

  1. val intent = Intent(this, MyActivity2::class.java) intent.putExtra("foo", "FOO") intent.putExtra("bar", 333) startActivity(intent)

    ・口伝でのみ伝えられる、必須パラメータ ・パラメータの型は、ムラハチレベルの掟
  2. inline fun <reified T : Activity> Context.startActivity( noinline extras: (Intent.()

    -> Unit)? = null) { val intent = Intent(this, T::class.java) extras?.let { intent.extras() } startActivity(intent) }
  3. companion object { const val EXTRA_FOO = "foo" const val

    EXTRA_BAR = "bar" fun showFoo(context: Context, foo: String, bar: Int) { context.startActivity<MyActivity2> { putExtra(EXTRA_FOO, foo) putExtra(EXTRA_BAR, bar) } } }
  4. companion object { const val EXTRA_FOO = "foo" const val

    EXTRA_BAR = "bar" const val EXTRA_HOGE = "foo" const val EXTRA_FUGA = "bar" fun showFoo(context: Context, foo: String, bar: Int) { context.startActivity<MyActivity2> { putExtra(EXTRA_FOO, foo) putExtra(EXTRA_BAR, bar) } } fun showHoge(context: Context, hoge: String, fuga: String) { context.startActivity<MyActivity2> { putExtra(EXTRA_HOGE, hoge) putExtra(EXTRA_FUGA, fuga) } } } 受け付けるパターンが増えると
  5. companion object { const val EXTRA_FOO = "foo" const val

    EXTRA_BAR = "bar" const val EXTRA_HOGE = "foo" const val EXTRA_FUGA = "bar" fun showFoo(context: Context, foo: String, bar: Int) { context.startActivity<MyActivity2> { putExtra(EXTRA_FOO, foo) putExtra(EXTRA_BAR, bar) } } fun showHoge(context: Context, hoge: String, fuga: String) { context.startActivity<MyActivity2> { putExtra(EXTRA_HOGE, hoge) putExtra(EXTRA_FUGA, fuga) } } }
  6. override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main2) when { intent.extras.containsKey(EXTRA_FOO)

    && intent.extras.containsKey(EXTRA_BAR) -> { // foo pattern } intent.extras.containsKey(EXTRA_HOGE) && intent.extras.containsKey(EXTRA_FUGA) -> { // hoge pattern } else -> throw IllegalArgumentException("staticメソッドから起動してね ") } }
  7. private sealed class Extras { data class Foo(val foo :

    String, val bar : Int) : Extras(), Parcelable { companion object { @JvmField @Suppress("unused") val CREATOR: Parcelable.Creator<Foo> = object : Parcelable.Creator<Foo> { override fun createFromParcel(source: Parcel): Foo = Foo(source) override fun newArray(size: Int): Array<Foo?> = arrayOfNulls(size) } } protected constructor(parcelIn: Parcel) : this(parcelIn.readString(), parcelIn.readInt()) override fun writeToParcel(dest: Parcel?, flags: Int) { dest?.writeString(foo) dest?.writeInt(bar) } override fun describeContents(): Int = 0 } data class Hoge(val hoge : String, val fuga : String) : Extras(), Parcelable { companion object { @JvmField @Suppress("unused") val CREATOR: Parcelable.Creator<Hoge> = object : Parcelable.Creator<Hoge> { override fun createFromParcel(source: Parcel): Hoge = Hoge(source) override fun newArray(size: Int): Array<Hoge?> = arrayOfNulls(size) } } protected constructor(parcelIn: Parcel) : this(parcelIn.readString(), parcelIn.readString()) override fun writeToParcel(dest: Parcel?, flags: Int) { dest?.writeString(hoge) dest?.writeString(fuga) } override fun describeContents(): Int = 0 } }
  8. private sealed class Extras { data class Foo(val foo :

    String, val bar : Int) : Extras(), Parcelable { companion object { @JvmField @Suppress("unused") val CREATOR: Parcelable.Creator<Foo> = object : Parcelable.Creator<Foo> { override fun createFromParcel(source: Parcel): Foo = Foo(source) override fun newArray(size: Int): Array<Foo?> = arrayOfNulls(size) } } protected constructor(parcelIn: Parcel) : this(parcelIn.readString(), parcelIn.readInt()) override fun writeToParcel(dest: Parcel?, flags: Int) { dest?.writeString(foo) dest?.writeInt(bar) } override fun describeContents(): Int = 0 } data class Hoge(val hoge : String, val fuga : String) : Extras(), Parcelable { companion object { @JvmField @Suppress("unused") val CREATOR: Parcelable.Creator<Hoge> = object : Parcelable.Creator<Hoge> { override fun createFromParcel(source: Parcel): Hoge = Hoge(source) override fun newArray(size: Int): Array<Hoge?> = arrayOfNulls(size) } } protected constructor(parcelIn: Parcel) : this(parcelIn.readString(), parcelIn.readString()) override fun writeToParcel(dest: Parcel?, flags: Int) { dest?.writeString(hoge) dest?.writeString(fuga) } override fun describeContents(): Int = 0 } } 論外
  9. private sealed class Extras { @Parcelize data class Foo(val foo

    : String, val bar : Int) : Extras(), Parcelable @Parcelize data class Hoge(val hoge : String, val fuga : String) : Extras(), Parcelable } ・Kotlin Android Extensionsで、Parcelableは簡単に作れる ・experimental注意
  10. companion object { fun showFoo(context: Context, foo : String, bar

    : Int) { context.startActivity<MyActivity2> { putExtra(EXTRAS_KEY, Extras.Foo(foo, bar)) } } fun showHoge(context: Context, hoge : String, fuga : String) { context.startActivity<MyActivity2> { putExtra(EXTRAS_KEY, Extras.Hoge(hoge, fuga)) } } }
  11. override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main2) val extras =

    intent.getParcelableExtra<Parcelable>(EXTRAS_KEY) ?: throw IllegalArgumentException("staticメソッドから起動してね ") when(extras) { is Extras.Foo -> { // foo pattern } is Extras.Hoge -> { // hoge pattern } } }
  12. override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main2) val extras =

    intent.getParcelableExtra<Parcelable>(EXTRAS_KEY) ?: throw IllegalArgumentException("staticメソッドから起動してね ") when(extras) { is Extras.Foo -> { // foo pattern } is Extras.Hoge -> { // hoge pattern } } }
  13. class ExtraParcelable : ReadOnlyProperty<Activity, Parcelable> { override fun getValue(thisRef: Activity,

    property: KProperty<*>) : Parcelable { return thisRef.intent.getParcelableExtra(EXTRAS_KEY) ?: throw IllegalArgumentException("extra parameter ${property.name} is null") } }
  14. private val extras by ExtraParcelable() override fun onCreate(savedInstanceState: Bundle?) {

    super.onCreate(savedInstanceState) setContentView(R.layout.activity_main2) when(extras) { is Extras.Foo -> { // foo pattern } is Extras.Hoge -> { // hoge pattern } } }