반응형
데이터를 전달하는 방법은 여러가지가 있습니다.
오늘은 bundleOf를 이용해 Activity에서 Fragment로 데이터를 전달하는 방법에 대해 알아보도록 하겠습니다
Activity에서 값 저장 방법
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private val loginDialog = LoginDialog() | |
val bundle = bundleOf("key" to value) | |
loginDialog.arguments = bundle |
우선 Fragment의 객체를 생성해 줍니다
이때 중요한건 객체를 중복해서 생성하게 되면 각각의 다른 객체기 때문에 저장한 값을 못 불러오게 됩니다!
bundle에 값을 집어 넣습니다
플러스로 번들에 값을 넣을때
그리고 fragment의 arguments에 bundle을 저장해줍니다
Fragment에서 저장된 값 불러오는 방법
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
arguments?.getInt("key") |
저장한 값을 불러오는 방법은 간단합니다.
해당 Fragment에 가서 arguments.getInt("설정한 키")로 값을 불러올수 있습니다
물론 getInt 이외에도 getString 등등 다 가능합니다
반응형
'Android' 카테고리의 다른 글
Jetpack Compose에 대해 간단하게 알아보자! (0) | 2021.09.09 |
---|---|
Android Studio 하위 패키지가 겹쳐지는 현상 (0) | 2021.08.27 |
의존성 주입이란? (0) | 2021.08.24 |
사진 선택 후 RecyclerView 갱신 (0) | 2021.08.11 |
Android floating action button 아이콘 색깔 변경 (0) | 2021.07.02 |