일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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
- cocoapods
- SWIFT
- Constraint
- InputTypes
- linearlayout
- Android
- ios
- 스택관리
- 데이터바인딩
- Lottie
- Drawable
- 액티비티
- DataBinding
- 화면전환
- xcworkspace
- ImageView
- 앱빌드
- 안드로이드
- lateInit
- 리소스
- EditText
- 코코아팟
- API Level
- LayoutEditor
- button
- 코틀린
- kotlin
- gradle
- 프래그먼트
- 프로젝트생성
Archives
- Today
- Total
코코아의 우당탕탕 개발일지
[Android/Kotlin] Activity와 Fragment 본문
작업 및 백 스택 이해 | Android 개발자 | Android Developers
일반적으로 앱에는 여러 활동이 포함됩니다. 각 활동은 사용자가 실행할 수 있는 특정 종류의 작업을 중심으로 설계되어야 하며 다른 활동을 시작할 수 있어야 합니다. 예를 들어 이메일 앱에는
developer.android.com
안드로이드 앱에서 뒤로가기를 눌렀을 때.. 제대로 이전 화면이 종료되지 않고 겹겹이 쌓여있던 걸 경험한 적이 있다. Activity와 Fragment의 특성을 제대로 고려해주지 않았기 때문에 발생한 문제였다고 생각한다.
activityA -> activityB 화면 전환의 경우 액티비티가 "대체"되지만,
fragmentA -> fragmentB 화면 전환의 경우 액티비티 위에 프레그먼트를 겹겹이 쌓아올리는 것이 가능하다.
- activity 공식 문서: https://developer.android.com/guide/components/activities/intro-activities?hl=ko
- fragment 공식 문서: https://developer.android.com/guide/components/fragments?hl=ko
- Android Developer - Fragment
이전 레이아웃에 프래그먼트를 추가
supportFragmentManager.beginTransaction()
.add(R.id.fragment_container, firstFragment).commit()
한 프래그먼트를 다른 프래그먼트로 교체
프래그먼트 교체 절차는 프래그먼트 추가 절차와 비슷하지만, add() 대신 replace() 메서드를 사용해야 한다.
프래그먼트 교체/삭제와 같은 Fragment Transaction을 실행할 때는 사용자가 뒤로 이동하여 변경을 '실행취소'하도록 허용하는 것이 적절한 경우가 많다. FragmentTransaction을 통해 사용자가 뒤로 이동할 수 있게 하려면, FragmentTransaction을 커밋하기 전에 addToBackStack()을 호출해야 한다.
// Create fragment and give it an argument specifying the article it should show
val newFragment = ArticleFragment()
Bundle args = Bundle()
args.putInt(ArticleFragment.ARG_POSITION, position)
newFragment.arguments = args
val transaction = supportFragmentManager.beginTransaction().apply {
// Replace whatever is in the fragment_container view with this fragment,
// and add the transaction to the back stack so the user can navigate back
replace(R.id.fragment_container, newFragment)
addToBackStack(null)
}
// Commit the transaction
transaction.commit();
'[Android] > 학습' 카테고리의 다른 글
[Android/Kotlin] 안드로이드 학습 2.2 - 사용자 상호작용 (0) | 2022.11.01 |
---|---|
[Android/Kotlin] 안드로이드 학습 2.1 - Layout Editor을 이용한 LinearLayout (0) | 2022.10.31 |
[Android/Kotlin] 안드로이드 학습 1.3 - 이미지 리소스 및 호환성 (0) | 2022.10.27 |
[Android/Kotlin] 안드로이드 학습 1.2 - 기본 Android 프로젝트 분석 (0) | 2022.10.25 |
[Android/Kotlin] 안드로이드 학습 1.1 - 프로젝트 생성 (0) | 2022.10.23 |