Fragment: A Fragment Is Like A Sub-Activity
Fragment: A Fragment Is Like A Sub-Activity
Fragment: A Fragment Is Like A Sub-Activity
Fragment
What is fragment?
AFragmentrepresents 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 (sort
of like a "sub activity" that you can reuse in
different activities).
Like :
<fragment
android:name="com.example.fragmenttest1.FirstFragment"
android:id="@+id/headlines_fragment"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent" />
FirstFragment.java
import
import
import
import
import
android.app.Fragment;
android.os.Bundle;
android.view.LayoutInflater;
android.view.View;
android.view.ViewGroup;
First.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginRight="60dp"
android:layout_marginTop="186dp"
android:text="Button" />
</RelativeLayout>
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<fragment android:name="com.example.fragmenttest1.FirstFragment"
android:id="@+id/headlines_fragment"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent" />
<fragment android:name="com.example.fragmenttest1.SecondFragment"
android:id="@+id/article_fragment"
android:layout_weight="2"
android:layout_width="0dp"
android:layout_height="match_parent" />
</LinearLayout>
Creating a Fragment
To create a fragment, you must create a subclass of Fragment(or an existing subclass of it).
TheFragment class has code that looks a lot like anActivity.
It contains callback methods similar to an activity, such asonCreate(),onStart(),onPause(), and
onStop().
<fragment android:name="com.example.news.ArticleReaderFragment"
android:id="@+id/viewer"
android:layout_weight="2"
android:layout_width="0dp"
android:layout_height="match_parent" />
Managing Fragments
To manage the fragments in your activity, you need to use
FragmentManager. To get it, callgetFragmentManager()from your
activity.
Some things that you can do withFragmentManagerinclude:
Get fragments that exist in the activity, withfindFragmentById()(for
fragments that provide a UI in the activity layout) orfindFragmentByTag
()(for fragments that do or don't provide a UI).
Pop fragments off the back stack, withpopBackStack()(simulating
aBackcommand by the user).
Register a listener for changes to the back stack, with
addOnBackStackChangedListener ().
For more information about these methods and others, refer to the
FragmentManagerclass documentation.
As demonstrated in the previous section, you can also use
FragmentManagerto open aFragmentTransaction, which allows you to
perform transactions, such as add and remove fragments.
example,
here's how you can replace one fragment with another, and
preserve the previous state in the back stack:
// Create new fragment and transaction
Fragment newFragment = new ExampleFragment();
FragmentTransaction transaction =
getFragmentManager().beginTransaction();
// Replace whatever is in the fragment_container view with this
fragment,
// and add the transaction to the back stack
transaction.replace(R.id.fragment_container, newFragment);
transaction.addToBackStack(null);
// Commit the transaction
transaction.commit();