티스토리 뷰
Fragment
Dexx 2015. 12. 4. 01:57[최소 구현 함수]
- onCreate() : fragment가 생성될 때 호출 함.
- onCreateView() : fragment가 UI를 최초로 그릴때 호출. fragment layout의 루트인 View를 리턴. UI가 없을 경우 null 리턴.
- onPause() : fragment를 떠날 때 호출. 보통 저장을 수행
[Runtime에 Fragment를 Activity에 추가]
- FragmentManager를 사용하여 FragmentTransaction을 통해 fragment를 런타임에 컨트롤
- Fragment를 제거하거나 replace하려면 initial fragment를 onCreate() 함수에 추가
- 런타임에 Fragment를 사용하려면 fragment가 살 수 있는 View를 가져야 함
- 보통의 레이아웃 대신 FragmentLayout을 주로 사용
getSupportFragmentManager().beginTransaction().add(R.id.fragment_container, userFragment).commit();
[Fragment 대체]
- add() 대신 replace()를 사용
- fragment transaction을 쓸때는 사용자가 back button으로 undo 할 수 있음
- 이것을 허용하려면 commit전에 addToBackStack()을 콜 해야 함
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
// Fragment 컨테이너에 있는 fragment를 대체. back button 가능하도록 함
// replace(containerID, Fragment fragment, @Nullable String tag)
transaction.replace(R.id.fragment_container, userFragment);
transaction.addToBackStack(null);
transaction.commit();
[Fragment간 통신]
- fragment간의 통신은 액티비티를 통해서만 가능. fragment간의 direct한 통신은 불가능.
singleTon을 이용한 값 전달은 가능.
- fragment에 인터페이스를 정의하고, 그것을 activity안에 구현
- 해당 fragment는 구현을 onAttach()라이프 사이클에서 capture함
원문 : http://egloos.zum.com/killins/v/3005780
'Android 개발 메모' 카테고리의 다른 글
setLayoutParams(), getLayoutParams() (0) | 2015.12.04 |
---|---|
Activity에 Fragment 추가하기 (0) | 2015.12.04 |
getClass() (0) | 2015.12.04 |