![Fragments in Android](https://mobikul.com/wp-content/uploads/2023/12/fragments-android-1-1024x512.png)
Fragments are a part of an activity with a particular behavior. You can say it is some sort of sub-activity. It is a modular and reusable part of an activity, having its own life cycle, and is still dependent on activity. You can add, remove, hide, or show the fragment according to your need in the activity.
You can use single as well as multiple, static as well as dynamic fragments in your activity based on your requirement. So firstly analyze your requirements and then use what suits you better.
A fragment is managed in an activity using FragmentManager for versions later Honeycomb below that the functionality is handled by the FragmentActivity class. You can get a Fragment manager object in an activity using
1 |
FragmentManager fragment_manager_object= getFragmentManager(); |
Fragment Transition:
Fragment Transition work is to perform operations on fragments. When a scene changes, a Transition has two main responsibilities −
- Capture the state of each view in both the start and end scenes.
- Create an Animator based on the differences that will animate the views from one scene to the other.
Users can add default animation and custom animation in a fragment using Fragment Transaction.
Fragment Class:
As we have mentioned a fragment has its life cycle as well as layout, so all the operations you want to perform are performed in their own .class file. You have to extend your MyFragment.class file from android.app.Fragment class. Then you have to override its lifecycle methods to get the desired functionality.
onAttach(): It is the first method that is called.
oncreate(): It’s mainly used to initialize all components.
onCreateView(): It’s called when it’s time draw the layout.
1 2 3 4 5 6 7 |
public static class MyFragment extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment return inflater.inflate(R.layout.my_fragment_layout, container, false); } } |
onActivityCreated(): It is called when the activity is completely loaded and is ready for user interaction.
onPause(): It’s called when the user moves away from the screen and it is backstacked.
Static fragments:
Static fragments are added by adding a fragment tag in the activity layout XML file.
1 2 3 4 5 6 |
<fragment android:id="@+id/my_fragment" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" class="<Your_package>.<your_fragment_class>" /> |
You can get the fragment from its ID and perform operations like show or hide on it ( You can’t remove a static fragment from your activity).
1 2 3 4 5 6 7 8 9 |
Fragment fragment = fm.findViewById(R.id.my_fragment); FragmentTransaction ft = getFragmentManager().beginTransaction(); if (fragment.isHidden()) { ft.show(fragment); } else { ft.hide(fragment); } ft.commit(); |
Dynamic fragments:
Dynamic fragments are fragments loaded dynamically in the place of any layout (say container) in your activity layout XML. These require add() to be loaded in your activity and remove() will remove them from your activity.
1 2 3 4 5 |
<LinearLayout android:id="@+id/fragment_container" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"/> |
Then you can add your fragment in the container as:
1 2 3 4 5 |
FragmentManager fm= getFragmentManager(); FragmentTransaction fragmentTransaction = fm.beginTransaction(); MyFragment my_fragment = new MyFragment(); fragmentTransaction.add(R.id.fragment_container, my_fragment, "start"); fragmentTransaction.commit(); |
When operations are to be performed on the fragment you can find the fragment using the tag as passed in add().
1 |
add (int containerViewId, Fragment fragment, String tag) |
1 |
Fragment fr= getFragmentManager.findViewByTag("start"); |
So these are the basics about fragments.
You can add many functionalities in a fragment and use them as per your requirement and give your application a nice touch by adding animations to it.
Also when fragments are hidden using hide(), their state is saved so if you want to show the user its last state you can hide the fragment rather than removing it but please note that if the activity is finished (destroyed) so do the fragments associated with the activity.
Official Documentation -> https://developer.android.com/guide/fragments