Skip to content
Search

Android.app.Fragment

lastmod 2018-08-17created 2016-03-034 edits

A Fragment is a smaller unit that can define UI or processing inside an Activity. You can place multiple Fragments in one Activity or reuse one Fragment across several Activities. So you can think of a Fragment as a small module that makes up an Activity. It has its own lifecycle separate from the Activity, handles its own events, and can be added and removed dynamically while an Activity is running.

Fragments were introduced in Honeycomb (Android 3.0, API level 11) as a UI design approach for tablets. They help with app UI design so that screens of various sizes can show more UI or handle more work. Fragments split an Activity's layout into parts, so developers can easily reuse them on screens of different sizes without heavily restructuring the View hierarchy. By splitting an Activity into modules this way, the Activity can change its layout dynamically at runtime and save those changes to the back stack, which also makes integration with the back button easy.

Fragment Lifecycle

Fragment_lifecycle.png

Features of Fragments

  • A fragment is a piece of an activity, or a subactivity.
  • Multiple fragments can be combined in one activity to build a multi-pane UI.
  • One fragment can be reused across multiple activities.
  • A fragment has its own lifecycle.
  • Receives input events and can be added or removed dynamically while the activity is running.
  • Can only exist inside an activity; cannot exist on its own.
  • Can exist alongside other views inside an activity.
  • Can use the back stack.
  • Must have a default constructor.

What is FragmentTransaction?

Description of Fragment Transaction.

add(int, Fragment, String)
Adds the Fragment to the FragmentManager.
remove(Fragment)
Removes the fragment if it exists. It can no longer be found in the FragmentManager.
hide(Fragment)
Hides the fragment if it exists. It is still managed by the FragmentManager and can be shown again later with show.
replace(int container, Fragment, String tag)
Calls remove(Fragment) on the fragment that exists in the container under the tag name, then calls add(int,Fragment, tag) with a Fragment under the same tag name. In short, it removes the fragment with the tag name in the container and then adds.
detach(Fragment)
Detaches the fragment if it exists. The difference from remove is that remove deletes it completely from the FragmentManager, while with detach it stays on the stack. It can be shown again later by calling attach, but since it is recreated in the View hierarchy, it doesn't seem much different from remove.
attach(Fragment)
Reattaches the Fragment to the View hierarchy if it exists.
setCustomAnimations(int enter, int exit, int popEnter, int popExit)
Specifies the entering and exiting animation of the Fragment. The popEnter and popExit animations specify the animation that runs when popped from the back stack.

Showing/hiding a Fragment

To show a Fragment, do the following.

FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.show(fragment);
ft.commit();

To hide a Fragment, do the following.

FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.hide(fragment);
ft.commit();

To add an animation, do the following.

ft.setCustomAnimations(숨기는 애니메이션 xml 자원,보여주는 에니메이션 xml 자원);

It can be customized in this form.

Example

To add a Fragment programmatically:

FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
ExampleFragment fragment = new ExampleFragment();
fragmentTransaction.add(R.id.fragment_container, fragment);
fragmentTransaction.commit();

To replace a specific Layout resource with a given Fragment:

Fragment fragment = new Fragment;
getSupportFragmentManager().beginTransaction().replace(R.id.content_frame, fragment).commit();

Adding a Fragment without UI

A Fragment without UI has no ID set on a View, so it must be found and added through an ID set via a tag. For this, use the FragmentTransaction.add(Fragment, String) function. The second argument assigns a unique string ID as the Fragment's tag. When a Fragment is added with this function, it is not tied to the Activity's View, so the Fragment's onCreateView() function is not called and you don't need to implement it.

Assigning an ID with a tag is not limited to Fragments without UI; a Fragment with UI can also be given a string ID via a tag. But if there is no UI, a string ID must be assigned only through a tag so that the findFragmentByTag() function can be used to find the Fragment from the Activity.

Favorite site

References


  1. Android_fragment_life_cycle.pdf ↩

  2. Fragment_basic_turotial.pdf ↩

  3. Android_app_develop_basic-fragment_basic.pdf ↩