Android Activity LifeCycle
아래 순서도 중요!!
함수 | 설명 |
onCreate() | Activity가 생성 될 때 최초 1회, 가장 먼저 실행되는 함수 기본 애플리케이션의 최초 로직을 작성할 때 본 함수에서 수행 화면 방향 전환 등으로 Destroy된 이전 Activity 상태에 대한 복원을 위해 savedInstanceState를 가져오는 작업을 수행 ex) 데이터의 binding, ViewModel의 연결, 클래스의 인스턴스화 |
onStart() | onCreate() 함수가 실행된 후, onStart() 함수가 실행 Activity가 사용자에게 표시되며 상호작용 준비 앱이 UI를 관리하는 코드를 본 함수에서 초기화 ex) 리스너 등록 |
onResume() | onStart() 함수가 실행된 후, onResume() 함수가 실행 이 때에는 앱과 사용자가 클릭 등의 상호작용이 가능한 시점 onCreate, onStart의 경우와 달리 사용자가 Home 버튼을 누르는 등 앱의 포커스가 사라지지 않는 한 계속 onResume 상태에 머무르게 된다. 이 때에는 앱이 사용자에게 보이는 동안 실행해야 하는 모든 기능을 활성화 하게 된다. ex) 카메라 미리보기 시작, 사용자에게 보여질 데이터 로드 |
onPause() | onResume 상태에서 사용자가 Activity를 떠날 때(화면이 사용자 눈에 보이지 않는 상태에) 호출됨 앱 사용 도중 전화를 받거나, 다른 앱으로 이동 할 때가 그 예 컴포넌트들이 사용자에게 보여지지 않을 때 사용 할 필요가 없는 기능들을 정지 할 수 있게 작성 임시로 현재 앱에서 소멸 될 수 있는 값들을 저장 할 수 있도록 한다. ex) 카메라 미리보기 정지, Shared Preferences를 통한 현재 값 임시 저장 |
onStop() | 사용자가 Activity를 떠나 사용자에게 보여지지 않을 때 onPause 이후 호출되는 함수 예를 들어, 새로 시작된 다른 동작이 사용자의 화면을 완전히 차지 했을 때 호출 이 때에는 컴포넌트들이 사용자에게 보여지지 않을 때 사용 할 필요가 없는 기능들을 정지 할 수 있게 조치 ex) 카메라 미리보기 정지 |
onRestart() | onStop() 상태에서 다시 사용자가 앱의 Activity로 돌아왔을 때 실행 onRestart가 수행된 후, 다시 onStart() -> onResume() 순서를 진행 (onPause()에서 저장된 값 복구) |
onDestroy() | lifecycle에서 해당 Activity가 완전히 소멸 됐을 때 실행 예를 들어 finish()를 통해 Activity가 소멸 됐을 경우, 화면 전환으로 인해 시스템에서 일시적으로 Activity를 소멸시키는 경우 onPause, onStop에서 해제하지 않은 모든 리소스를 해제하는 코드 작성 |
cat: concatenate 의 약자.
Fragment : activity보다 가벼운(메모리가 덜 필요한) class
Android Fragment Lifecycle
1. statically added Fragment
2. dynamically added Fragment
다음 글에서는 Fragment를 사용해 코드를 작성해보겠습니다.
'수업' 카테고리의 다른 글
모바일프로그래밍 - lifeCycle (Dynamic Fragment) (0) | 2023.03.28 |
---|---|
모바일프로그래밍 - LifeCycleFragment (0) | 2023.03.28 |
모바일프로그래밍 - Android 앱 개발 기초 / mile을 km로 변환하는 앱 (0) | 2023.03.27 |
컴퓨터그래픽스 - 5. OpenGL 개요, 구성 요소, freegult 설치 (0) | 2023.03.27 |
컴퓨터그래픽스 - 4. 그래픽스 API (+Cortona3D 설치) (0) | 2023.03.27 |