2017-06-05 7 views

答えて

5

TestActivity.kt

class TestActivity : Activity() { 
    val DEVOTIONAL_RESPONSE: String = "DEVOTIONAL_RESPONSE" 
    var holdr: Holdr_ActivityTest? = null 

    override fun onCreate(savedInstanceState: Bundle?) { 
     super.onCreate(savedInstanceState) 
     setContentView(R.layout.activity_test) 
     holdr = Holdr_ActivityTest(findViewById(android.R.id.content)) 

     //create response 
     var response: DevotionalResponse = DevotionalResponse() 
     fillInResponse(response) 

     //write it to a Bundle 
     var newBundle: Bundle = Bundle() 
     newBundle.putParcelable(DEVOTIONAL_RESPONSE, response) 

     //read it from the new Bundle 
     var newResponse: DevotionalResponse = newBundle.getParcelable(DEVOTIONAL_RESPONSE) 

     //check that they're the same 
     assert(newResponse.getDevotionals().get(0).getContent().equalsIgnoreCase(response.getDevotionals().get(0).getContent())) 
    } 
} 

DevotionalResponse.kt

class DevotionalResponse() : Parcelable { 

    @Expose 
    var devotionals = arrayListOf<Devotional>(); 

    constructor(in: Parcel) : this() { 
     in.readTypedList(this.devotionals, Devotional.CREATOR) 
    } 

    override fun describeContents(): Int = 0 

    override fun writeToParcel(dest: Parcel, flags: Int) { 
     dest.writeTypedList(devotionals) 
    } 

    companion object { 
     @JvmField val CREATOR = object : Parcelable.Creator<DevotionalResponse>() { 
      override fun createFromParcel(in: Parcel): DevotionalResponse = 
        DevotionalResponse(in) 

      override fun newArray(size: Int): Array<DevotionalResponse?> = 
        arrayOfNulls(size) 
     } 
    } 
} 

完全なソリューションは、このリンクに行く

Kotlin Object Example

+0

*この*は、オブジェクトを渡すためのAndroidの方法です。受け入れられた回答としてマークする必要があります。 –

+0

@ Miha_x64 yaaaしかし、あなたはkotlinでそれをすることができます – Sendy

+0

ああ、私はそれがJavaで気づいたことはありません。 KotlinとJavaの切り替えは、私にとっては絶対にシームレスです。 :) –

関連する問題