Before this, let me ask you what is the running order of onPause in called 0? And when can I override the onPause in called 1? Show Many people get confused when and what method should they call when they start the Android development. This article is the quick guide give you an overview on this. Two main Views in the Android, Activity and Fragment. ActivityHere is a demo on the running order: The output of first start up: onCreate in called The output of go back to mobile home screen: onPause in called The output of going back to app from background: onCreate in called FragmentThe output of first start up: running onCreateView The output of go back to mobile home screen: running onPause The output of going back to app from background: running onCreateView Update on Activity lifecycle methods:
onPause in called 3: The current Activity is being restarted, from invisible to visible.
onPause in called 5 Activity is being stopping but still visible. Normally the onPause in called 6 will be called next to it. onPause in called 7 can't have too much heavy duty work, as new Activity's onResume() starts only when onPause() finished.
onPause in called 6: Activity is about to be destroyed. You can do some clean up work here, but not too heavy. 음 제목은 이렇게 지었지만, 4대 컴포넌트중 생명주기를 가지고 있는 컴포넌트는 크게 두가지밖에 없습니다. 오늘은, 그 Activity, Service, 그리고 Fragment 생명주기에 대해 다루어 보도록 하겠습니다. 1. Activity 생명주기아래 엑티비티 생명주기 다이어그램을 참고하면서 이야기를 해보도록 합시다. 일단, 다이어그램을 보면 알수있듯, Activity의 상태에 따라 함수들이 호출됩니다. 아래는 Activity의 상태에 따라 호출되는 함수들에 대해 정리했습니다. onCreate()
onStart()
onResume()
onPause()
onStop()
onDestroy()
onRestart()
2. Fragment 생명주기앞서 4대 컴포넌트 포스팅에서 말씀드렸다시피, Fragment는 그럼 Fragment의 생명주기 설명도 다이어그램으로 시작하겠습니다. 이미지 출처onAttach()
onCreate()
onCreateView()
onActivityCreated()
onStart()
onResume()
onPause()
onStop()
onDestroyView()
onDestroy()onDetach()
Plus Alpha1-1. Activity의 onCreate의 기능을 Fragment에서는 두가지로 분리했습니다.1-2. 그리고, Activity의 onDestroy의 기능도 Fragment에서는 두가지로 분리했습니다.
2. Fragment의 생명주기와 Activity의 생명주기는 이렇게 움직입니다.Fragment Commit Activity: onCreate() Fragment1: onAttach() Activity: onStart() New Fragment Commit Activity: Fragment2 commit() Fragment1: onPause() Fragment2: onCreateView() onBackPressed called Fragment2: onPause() Activity: onPause() Fragment2: onStop() Activity: onStop() Fragment2: onDestroyView() Activity: onDestroy() 직접 로그를 찍어본 결과입니다. 위 로그를 보고 꼭 결론을 도출해 보시기 바랍니다. 3. Service 생명주기서비스는 아래의 두가지 case의 생명주기를 가집니다. 그럼, Service의 생명주기 다이어그램으로 시작합니다. 이미지 출처다이어그램을 보시다시피 Framgnet나 Activity의 생명주기와 비교했을 때 굉장히 단순한 모습입니다. onRestart()7를 통해 호출된 서비스서비스 스스로가 서비스를 종료하거나, 클라이언트가 onRestart()9를 통해 호출된 서비스서비스에 연결된 클라이언트가 모두 이렇게 4대 컴포넌트 중, 생명주기를 가지고 있는 컴포넌트인 Activity, Service와 Fragment의 생명주기에 대해서 다루어 보았습니다. 혹시, 내용에서 잘못된 내용이나 부족한 내용에 대해서는 댓글로 남겨주시면 보완해 나가도록 하겠습니다. 다음에는 안드로이드 ACC(Android Architecture Components)에 대해 알아보겠습니다. [1] Listener interface를 implement 한 경우 : 일반적으로 Fragment에서 Activity에 있는 메소드 호출 시 사용하는 방법. |