[Android/Kotlin]intentにデータカテゴリを含める


必要なデータ・クラスをIntentに表示
📄 TestDataClass.kt
data class TestDataClass(
    var name: String,
    var age: Int
) : Serializable
拡張
  • シリアル化
  • 📄 MainActivity.kt
    override fun onCreate(savedInstanceState: Bundle?){
        ...
        val data = TestDataClass("장똑대", 20)
    
        val myIntent = Intent(this, SecondActivity::class.java)
        myIntent.putExtra("TestData", data)
        startActivity(myIntent)
    }
    📄 SecondActivity.kt
    override fun onCreate(savedInstanceState: Bundle?){
        // TestDataClass로 타입캐스팅
        val data = intent.getSerializableExtra("TestData") as TestDataClass
        Log.d("Name", data.name)
        Log.d("Age", data.age)
    }