Bundle

최대환·2024년 1월 3일
0

Bundle이란?

Bundle은 사실상 키-값 쌍의 컨테이너라고 볼 수 있습니다. Map과 비슷한 개념입니다. Bundle에는 여러 타입의 값들을 저장할 수 있습니다. 그리고 키를 사용하여 값을 넣거나 가져오면 됩니다.

사용법

Bundle 객체를 생성하고 putString 메서드를 이용해 Bundle에 데이터를 넣고 있습니다. 그리고 이 Bundle을 Intent에 넣어 다른 액티비티로 전달합니다

val intent = Intent(this, SecondActivity::class.java)
val bundle = Bundle()
bundle.putString("key", "Value")
intent.putExtras(bundle)
startActivity(intent)

데이터를 받을때에는 아래와 같이 Bundle에서 데이터를 가져옵니다.

val bundle = intent.extras
val value = bundle?.getString("key")

그리고 받고자하는 데이터를 전달하지 않으면 null값을 가져옵니다.

profile
나의 개발지식 output 공간

0개의 댓글