Fragment

Activity보다 작은 화면 단위.

태블릿의 가로로 긴 화면때문에 등장 하였다.

Fragment는 자체적인 lifecycle을 가지나 Activity의 Lifecycle에 영향을 받는다. Activity가 동작 중에 fragment의 추가, 삭제가 가능하다.

Fragment는 Activity처럼 Back Stack으로 쌓이진 않지만, 이를 기술적으로 지원할 수 있다.

// 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();

여기서 Transaction은 Activity 내에서 Fragment를 추가/삭제하는 변경사항을 의미한다. Back Stack을 적용하면 아래와 같이 된다.

위의 그림은 적용 후의 결과, 그 아래의 그림은 적용 전 기본적인 Fragment이다.

results matching ""

    No results matching ""