About Fragment
-Fragment는 자신만의 layout 및 lifecycle을 가진다.
-Running 중인 Activity 내에서 add/remove가능하다.
-하나의 Activity 여러개의 Fragment는 사용할 수 있다.
-하나의 Fragment는 여러 Activity에서 활용할 수 있다.
-Activity life-cycle과 매우 유사, Activity 멈추면 Fragment는 같이 멈춘다.
-UI없는 Fragment 구현가능하다.
Example) Single Fragment
public class LM_Fragment extends Fragment {
static final String TAG=LM_Fragment.class.getSimpleName();
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
Log.d(TAG, "onCreateView");
return inflater.inflate(R.layout.lm_fragment, container, false);
}
}
public class FragmentActivity extends Activity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Configuration config = getResources().getConfiguration();
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
if (config.orientation == Configuration.ORIENTATION_LANDSCAPE){
Fragment lm= new LM_Fragment();
fragmentTransaction.replace(android.R.id.content, lm);
} else {
Fragment pm= new PM_Fragment();
fragmentTransaction.replace(android.R.id.content, pm);
}
fragmentTransaction.commit();
}
}
lm_fragment.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:background="@color/fui_linkColor"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="LM_Fragment"
android:textColor="#000000"
android:textSize="20px" />
</LinearLayout>
</android.support.constraint.ConstraintLayout>
AndroidManifest.xml
<activity android:name=".FragmentActivity">
</activity>
출처 : https://www.tutorialspoint.com/index.htm
'android' 카테고리의 다른 글
| AudioTrack 정리 (0) | 2018.07.26 |
|---|---|
| MediaPlayer (0) | 2018.07.18 |
| Custom Intent로 app 실행하기 (0) | 2018.07.16 |
| tutorialspoint (0) | 2018.07.16 |
| custom view tutorial 따라하기 3 (0) | 2018.07.16 |