컴퓨터/이론: 안드로이드
[2017.09.27] 23. Fragment 사용 시, 알야아 할 점
heepie
2017. 9. 27. 20:36
1. Fragment의 Background는 Default로 투명
그래서 그림과 같이 Activity 위에 Fragment를 Add할 경우 뒤에 MainActivity도 비치게 된다.

Background Color를 설정해주며 다음과 같은 결과를 얻을 수 있다.

2. Default 설정의 Fragment는 백 스택에 쌓이지 않는다.
Activity가 관리하는 백 스택에 쌓이지 않아 Back Button 클릭 시 이전 화면으로 돌아가지 않는다.
백 스택 설정 전 |
백 스택 설정 후 |

| FragmentManager manager = getSupportFragmentManager(); // 2. 트랜젝션 관리자 설정 FragmentTransaction transaction = manager.beginTransaction(); // 3. 트랜젝션에 Fragment 추가(추가할 위치, 생선한 Fragment) transaction.add(R.id.stage, new ListFragment()); // 4. 트랜젝션 시작 transaction.commit(); | cs |
|

| FragmentManager manager = getSupportFragmentManager(); // 2. 트랜젝션 관리자 설정 FragmentTransaction transaction = manager.beginTransaction(); // 3. 트랜젝션에 Fragment 추가(추가할 위치, 생선한 Fragment) transaction.add(R.id.stage, new ListFragment()); // 4. 백 스택 설정 transaction.addToBackStack(null); // 5. 트랜젝션 시작 transaction.commit(); | cs |
|
3. Default 설정의 Fragment는 클릭이 가능하지 않다.
그래서 뒤에 존재해 보이지 않는 객체의 리스너가 실행된다.
Clickable 설정 전 |
Clickable 설정 후 |

|

|
| <FrameLayout android:layout_width="match_parent" android:layout_height="match_parent" android:background="#ffff00" tools:context=""> <!-- TODO: Update blank fragment layout --> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:text="ListFragment" android:textSize="30dp"/> </FrameLayout> | cs |
|
| <FrameLayout android:layout_width="match_parent" android:layout_height="match_parent" android:background="#ffff00" android:clickable="true" tools:context=""> <!-- TODO: Update blank fragment layout --> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:text="ListFragment" android:textSize="30dp"/> </FrameLayout> | cs |
|
※ FrameLayout은 상대적 위치를 나타낼 수 없다. (상대적 위치를 위해서는 RelativeLayout 사용)
FrameLayout의 위치를 설정하는 방법은 Layout_gravity를 통해 설정이 가능하다.
#Fragment 사용 #Fragment 알아야 할 점 #Fragment 주의 사항