Q-1 What is fragment and how we can create it in android application?
- A Fragment represents a behavior or a portion of user interface in an Activity.
- You can combine multiple fragments in a single activity to build a multi-pane UI and reuse a fragment in multiple activities.
- You can think of a fragment as a modular section of an activity, which has its own lifecycle, receives its own input events, and which you can add or remove while the activity is running.
- A fragment must always be embedded in an activity and the fragment’s lifecycle is directly affected by the host activity’s lifecycle.
- However, while an activity is running (it is in the resumed lifecycle state), you can manipulate each fragment independently, such as add or remove them.
- When you perform such a fragment transaction, you can also add it to a back stack that’s managed by the activity— each back stack entry in the activity is a record of the fragment transaction that occurred.
- The back stack allows the user to reverse a fragment transaction (navigate backwards), by pressing the Back button.
Adding a fragment to an activity
- Usually, a fragment contributes a portion of UI to the host activity, which is embedded as a part of the activity’s overall view hierarchy.
- There are two ways you can add a fragment to the activity layout:
Declare the fragment inside the activity’s layout file
- In this case, you can specify layout properties for the fragment as if it were a view. For example, here’s the layout file for an activity with two fragments:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="match_parent"> <fragment android:name="com.example.news.ArticleListFragment" android:id="@+id/list" android:layout_weight="1" android:layout_width="0dp" android:layout_height="match_parent" /> <fragment android:name="com.example.news.ArticleReaderFragment" android:id="@+id/viewer" android:layout_weight="2" android:layout_width="0dp" android:layout_height="match_parent" /> </LinearLayout>
- The android:name attribute in the specifies the Fragment class to instantiate in the layout.
- When the system creates this activity layout, it instantiates each fragment specified in the layout and calls the onCreateView() method for each one, to retrieve each fragment’s layout.
- The system inserts the View returned by the fragment directly in place of the element.
Programmatically add the fragment to an existing ViewGroup
- At any time while your activity is running, you can add fragments to your activity layout. You simply need to specify a ViewGroup in which to place the fragment.
- To make fragment transactions in your activity (such as add, remove, or replace a fragment), you must use APIs from FragmentTransaction.
- You can get an instance of FragmentTransaction from your Activity like this:
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
- You can then add a fragment using the add() method, specifying the fragment to add and the view in which to insert it. For example:
ExampleFragment fragment = new ExampleFragment();
- The first argument passed to add() is the ViewGroup in which the fragment should be placed, specified by resource ID, and the second parameter is the fragment to add.
- Once you’ve made your changes with FragmentTransaction, you must call commit() for the changes to take effect.