티스토리 뷰
1. Intent(+Serializable, Parcelable) 이용하는 방법
2. startActivityForResult 이용하는 방법
1. Intent(+Serializable, Parcelable) 이용하는 방법
SerializablePrivacy.kt
package com.jwsoft.kotlinproject
import java.io.Serializable
class SerializablePrivacy(
var height: Int,
var weight: Int
) : Serializable
ParcelableDetail.kt
package com.jwsoft.kotlinproject
import android.os.Parcel
import android.os.Parcelable
class ParcelableDetail(
var phone: String?,
var address: String?
) : Parcelable {
constructor(parcel: Parcel) : this(
parcel.readString(),
parcel.readString()
)
override fun writeToParcel(parcel: Parcel, flags: Int) {
parcel.writeString(phone)
parcel.writeString(address)
}
override fun describeContents(): Int {
return 0
}
companion object CREATOR : Parcelable.Creator<ParcelableDetail> {
override fun createFromParcel(parcel: Parcel): ParcelableDetail {
return ParcelableDetail(parcel)
}
override fun newArray(size: Int): Array<ParcelableDetail?> {
return arrayOfNulls(size)
}
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
tools:context=".MainActivity">
<Button
android:id="@+id/btnMoveToSub"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAllCaps="false"
android:text="Move To Sub Activity"
android:textSize="26dp"/>
</LinearLayout>
MainActivity.kt
package com.jwsoft.kotlinproject
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
btnMoveToSub.setOnClickListener {
val intent = Intent(this, SubActivity::class.java)
intent.putExtra("name", "James Kim")
intent.putExtra("age", 30)
intent.putExtra("serializablePrivacy", SerializablePrivacy(183, 80))
intent.putExtra("parcelableDetail", ParcelableDetail("010-1234-1234", "Seoul"))
startActivity(intent)
}
}
}
activity_sub.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
tools:context=".SubActivity">
<TextView
android:id="@+id/tvName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="30dp"
tools:text="이름"/>
<TextView
android:id="@+id/tvAge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="30dp"
tools:text="나이"/>
<TextView
android:id="@+id/tvHeight"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="30dp"
tools:text="키"/>
<TextView
android:id="@+id/tvWeight"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="30dp"
tools:text="몸무게"/>
<TextView
android:id="@+id/tvPhone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="30dp"
tools:text="핸드폰번호"/>
<TextView
android:id="@+id/tvAddress"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="30dp"
tools:text="주소"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="30dp"/>
</LinearLayout>
SubActivity.kt
package com.jwsoft.kotlinproject
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import kotlinx.android.synthetic.main.activity_sub.*
class SubActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_sub)
val name = intent.extras?.getString("name")
val age = intent.extras?.getInt("age")
val privacy = intent.extras?.get("serializablePrivacy") as SerializablePrivacy
val detail = intent.extras?.get("parcelableDetail") as ParcelableDetail
tvName.text = name
tvAge.text = age.toString()
tvHeight.text = privacy.height.toString()
tvWeight.text = privacy.weight.toString()
tvPhone.text = detail.phone
tvAddress.text = detail.address
}
}
2. startActivityForResult 이용하는 방법
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
tools:context=".MainActivity">
<TextView
android:id="@+id/tvText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="30dp"
tools:text="이름"/>
<Button
android:id="@+id/btnMoveToSub"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAllCaps="false"
android:text="Move To Sub"
android:textSize="30dp"/>
</LinearLayout>
MainActivity.kt
package com.jwsoft.kotlinproject
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : AppCompatActivity() {
companion object {
const val SUB_ACTIVITY_CODE = 1002
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
btnMoveToSub.setOnClickListener {
val intent = Intent(this, SubActivity::class.java)
startActivityForResult(intent, SUB_ACTIVITY_CODE)
}
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (requestCode == SUB_ACTIVITY_CODE && resultCode == RESULT_OK) {
val name = data!!.getStringExtra("name")
tvText.text = name
}
}
}
activity_sub.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
tools:context=".SubActivity">
<Button
android:id="@+id/btnMoveToMain"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAllCaps="false"
android:text="Move To Main"
android:textSize="30dp"/>
</LinearLayout>
SubActivity.kt
package com.jwsoft.kotlinproject
import android.app.Activity
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import kotlinx.android.synthetic.main.activity_sub.*
class SubActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_sub)
btnMoveToMain.setOnClickListener {
val intent = Intent(this, MainActivity::class.java)
intent.putExtra("name", "James Kim")
setResult(Activity.RESULT_OK, intent)
finish()
}
}
}
'Android > Kotlin' 카테고리의 다른 글
[Kotlin] Fragment에서 Activity로 데이터 전달 (0) | 2020.10.24 |
---|---|
[Kotlin] Activity에서 Fragment로 데이터 전달 (0) | 2020.10.24 |
[Kotlin] savedInstanceState를 이용하여 Activity 데이터 보존하기 (0) | 2020.10.15 |
[Kotlin] ViewPager2에서 본 Activity와 Fragment의 생명주기 (0) | 2020.10.06 |
[Kotlin] Activity와 Fragment 간의 ViewModel 공유 (0) | 2020.10.05 |
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- ViewModel
- fragment
- James Kim
- activity
- handler
- Livedata
- XML
- DataBinding
- 안드로이드
- 자바
- View
- Design Pattern
- Kotlin
- coroutine
- Intent
- ArrayList
- 코틀린
- CoordinatorLayout
- Vue.js #Vue.js + javascript
- JSONObject
- Android
- TabLayout
- java
- recyclerview
- 안드로이드 #코틀린 #Android #Kotlin
- 혀가 길지 않은 개발자
- MVVM
- Architecture Pattern
- ViewPager2
- JSONArray
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | 7 |
8 | 9 | 10 | 11 | 12 | 13 | 14 |
15 | 16 | 17 | 18 | 19 | 20 | 21 |
22 | 23 | 24 | 25 | 26 | 27 | 28 |
29 | 30 | 31 |
글 보관함