Notice
Recent Posts
Recent Comments
Link
| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 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 |
Tags
- 티스토리챌린지
- React
- docker
- Express
- 코테
- Jenkins
- Android
- 파이썬
- 오블완
- 오퍼월
- it
- nginx
- Next
- toml
- chrome
- kotlin
- 개발
- JavaScript
- TypeScript
- AWS
- EC2
- spring boots
- css
- NanoHttpd
- nuxt
- python
- react-native
- 코딩테스트
- 광고 id
- 백준
Archives
- Today
- Total
내맘대로 개발일지
[Kotlin] Activity 간에 데이터 주고받는 방법 본문

📱테스트 환경
Samsung Galaxy Android 13 • Android 10
😢 내가 겪은 문제
Activity 를 전환할 때 여기있는 일부 데이터도 같이 전송하고 싶었다. 근데 ... 정말 간단하다 ...!!
그래서 정말 간단하게 설명을 적어두고 간단한 코드를 기록하려고 한다 : ) 복사 붙여넣기만 해도 작동이 될 정도로 ㅎㅎ
데이터를 보낼 때
class SendingActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_sending)
val valueToSend = "Hello, TargetActivity!"
val intent = Intent(this, TargetActivity::class.java)
intent.putExtra("message", valueToSend)
startActivity(intent)
}
}
데이터를 받을 때
class TargetActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_target)
val receivedIntent = intent
val receivedMessage = receivedIntent.getStringExtra("message")
// Now you can use receivedMessage as needed
// (e.g., display it in a TextView)
}
}
와 !! 정말 쉽고 정말 간단하다 !!
근데 함수명에서 알다시피 String 타입을 받을 때 getStringExtra 를 사용하지만 다른 타입을 받을 때는 어떤 함수를 써야하냐 !!
타입이 Number 인 경우 : getIntExtra("intKey", defaultValue)
타입이 Boolean 인 경우 : getBooleanExtra("boolKey", defaultValue)
중요한건 보낼 때는 intent.putExtra() 를 사용하고 받을 때는 intent.getStringExtra() 를 사용하면 된다는 거다.
'Android > Kotlin' 카테고리의 다른 글
| [Kotlin] ByteArray -> Bitmap Image Resize (0) | 2023.08.09 |
|---|---|
| [Kotlin] This view is not constrained vertically: at runtime it will jump to the top unless you add a vertical constraint (0) | 2023.08.09 |
| [Kotlin] 앱 알림 보내기 (0) | 2023.08.09 |
| [Kotlin] Activity 이동 시 Animation 제거하는 방법 (0) | 2023.08.09 |
| [Kotlin] Splash Screen 만들기 (0) | 2023.08.09 |