티스토리 뷰
프래그먼트, Fragment
액티비티의 하위개념
자신의 UI와 생명주기를 자체적으로 갖고있는 컴포넌트
샌드위치(4.0) 부터 지원
개발자가 액티비티의 화면을 분할 (탐색기에서 왼쪽은 드라이브정보, 왼쪽은 파일정보)
Fragment 클래스를 상속받아 생성
onAttatch() : 프래그먼트가 액티비티와 연결될때 호출
onCreateView() : 프래그먼트의 계층뷰가 생성될때 호출 (프래그먼트가 그려질때)
onActivityCreated() : 액티비티의 onCreate()가 반환될때 호출
onDestroyView() : 프래그먼트의 계층 뷰가 제거 될때 호출
onDetach() : 프래그먼트가 액티비티로 부터 연결이 제거될때 호출
프래그먼트는 아래 3가지를 구현해야 한다.
onCreate()
onCreateView()
onPause() : 프래그먼트가 변경될때 호출되며 이거이 호출되었다고 해서 destory 되는것은 아이며, commit 해야 함
종류
DialogFragment
ListFragment
PreferenceFragment : 리스트 속성을 나타내며, 환경설정 등에 사용
프래그먼트에 UI 추가
onCreateView()를 구현해 UI를 View의 형태로 반환해야 한다.
// Layout을 이용해서 View 만들기
View addView = inflater.inflate(R.layout.example_fragment, container, false);
return addView;
액티비티에 프래그먼트 추가
1. Layout 리소스 이용
2. 코드로 추가
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
public class test extends ActionBarActivity {
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_test); FragmentManager fm = getFragmentManager(); FragmentTransaction ft = fm.beginTransaction(); 프래그먼트 클래스 f = new 프래그먼트 클래스(); ft.add(R.id.fragment_container, f); ft.commit(); } public static class 프래그먼트 extends ListFragment { ..... } } |
프래그먼트 관리 (추가, 삭제, 교체)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
public class test extends ActionBarActivity {
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_test); 프래그먼트 클래스 f = new 프래그먼트 클래스(); FragmentTransaction ft = getFragmentManager().beginTransaction(); ft.replace(R.id.fragment_container, f); ft.addToBackStack(null); // 프래그먼트 관리 ft.commit(); } public static class 프래그먼트 extends ListFragment { ..... } } |