">

Fragment


  Fragment는 Activity 내에서 사용할 수 있는 Layer라고 생각하시면 이해하기 쉬울 것 같습니다. 여러개의 Fragment를 하나의 Activity에 조합하여 사용하여 창이 여러개인 UI를 만들 수 있고, 하나의 Fragment를 여러 Activity에서 재사용 할 수 있습니다. 

  Fragment는 자체 생명 주기를 가지고 있습니다. Fragment는 항상 Activity내에 포함되어 있어야 하고, 생명주기는 Host Activity의 생명 주기에 직접적으로 영향을 받습니다. 원래 도화지가 없어졌는데 그 위에 Layer가 존재할 수 없겠죠? 예를 들어 Activity가 일시 정지 되는 경우 Activity안에 있는 모든 Fragment도 일시정지 되며 소멸 시에도 동일합니다. Fragment 트랜젝션을 수행할 때 Activity가 관리하는 백 스택에도 추가할 수 있습니다. 

  아래 그림은 Fragment를 사용하여 태블릿에서 조합하여 사용하거나, 스마트폰에서 분리해서 사용할 수 있다는 설명을 보여줍니다. 이하 더 자세한 설명은 개발자 페이지에서 확인해주세요. (https://developer.android.com/guide/components/fragments.html?hl=ko)

[출처: Google Developer]




구현


  Fragment는 일반적으로 Activity의 사용자 인터페이스의 일부로 사용되며 자체 레이아웃으로 Activity에 표현 가능합니다. 

Fragment에서 레이아웃을 사용하려면 반드시 onCreateView() 콜백 메서드를 구현해야 합니다. 그리고 이 메서드는 반드시 View를 반환해야 합니다. 


  다음 예는 Fragment의 서브 클래스로 example_fragment.xml 파일로부터 레이아웃을 읽어 옵니다. 

public static class ExampleFragment extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.example_fragment, container, false);
    }
}

  onCreateView()로 전달된 container 매개변수가 상위 ViewGroup이며 이는 Activity의 레이아웃으로부터 온 것입니다. 이 안에 Fragment 레이아웃이 삽입 됩니다. savedInstanceState는 true, false로 값을 정할 수 있는데 false 일 때는 레이아웃에 중복된 ViewGroup을 생성하지 못하게 하는 것입니다. 간단하게 말하면 각 각의 Fragment 레이아웃 중복 없이 사용하고 싶다면 savedInstanceState를 false로 사용해야 합니다. 



Fragment 관리


  Activity내에서 Fragment를 사용하기 위해서 FragmentManager을 사용해야 합니다. Activity에서 getFragmentManager()를 호출하면 됩니다. 

  ● FragmentManager fragmentManager = getFragmentManager();


  FragementManager을 통해 할 수 있는 일중에 몇 가지를 예를 들어보겠습니다. 

  ● Activity내에 존재하는 Fragment를 findFragmentById()로 가져오거나 findFragmentByTag()로 가져올 수 있습니다. 

  ● popBackStack()을 사용하여 Fragment를 백스택에서 꺼낼 수 있습니다. 

  ● 백스택 변경 내용이 있는지 확인하기 위해 addOnBackStackChangedListener()로 리스너를 등록할 수 있습니다. 



Fragment 트랜젝션


  FragmentTransaction의 인스턴스를 FragmentManager로부터 가져오는 방법은 아래와 같습니다. 

  ● FragmentManager fragmentManager = getFragmentManager(); 

  ● FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();


Transaction에서 add(), remove(), replace()와 같은 메서드를 사용할 수 있습니다. 그리고 마지막에 반드시 commit()를 호출해야 합니다. commit() 호출 전 해야할 것이 있는데 addToBackStack()입니다. fragment transaction은 activity가 관리하며 이를 통해 사용자가 back 버튼을 이용하여 이전 fragment로 돌아갈 수 있습니다. 

// Create new fragment and transaction
Fragment newFragment = new ExampleFragment();
FragmentTransaction transaction = getFragmentManager().beginTransaction();

// Replace whatever is in the fragment_container view with this fragment,
// and add the transaction to the back stack
transaction.replace(R.id.fragment_container, newFragment);
transaction.addToBackStack(null);

// Commit the transaction
transaction.commit();


Activity와의 통신


  Fragment에서 activity의 인스턴스에 엑세스 하려면 getActivity()를 사용합니다.

  ● View listView = getActivity().findViewById(R.id.list);


  반대로 activity에서도 fragment 안의 메서드를 호출할 수 있습니다. 이 때 FragmentManager을 사용하여 Fragment에 대한 참조를 가져와야 합니다. 

  ● ExampleFragment fragment = (ExampleFragment) getFragmentManager().findFragmentById(R.id.example_fragment);



정리

  Fragment는 activity위에 레이아웃을 그릴 수 있는 ViewGroup 중 하나입니다. 따라서 activity와 생애를 같이 합니다. Activity에서 Fragment를 다루려면 FragmentManager가 필요하고 getFragmentManager()을 통해 사용이 가능합니다. 그리고 fragment를 추가, 제거 할 때 즉 transaction이 발생할 때 FragmentTransaction을 사용합니다. 


  다음 글에서는 실제 코드를 가지고 어떻게 fragment를 활용했는지 알아보도록 하겠습니다. 

+ Recent posts