📱테스트 환경
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 |