Post on 14-Aug-2020
FRAGMENTS
Fragments
An activity is a container for views
When you have a larger screen device than a phone –like a tablet it can look too simple to use phone interface here.
FragmentsMini-activities, each with its own set of views
One or more fragments can be embedded in an Activity
You can do this dynamically as a function of the device type (tablet or not) or orientation
ALSO, you can reuse fragments --- like reuse of mini interfaces
Fragments
Fragments
Fragments were added to the Android API in
Honeycomb, API 11.
The primary classes related to fragments are:
android.app.Fragment
The base class for all fragment definitions
android.app.FragmentManager
The class for interacting with fragment objects inside an
activity
android.app.FragmentTransaction
The class for performing an atomic set of fragment
operations
FRAGMENTS
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 (sort of like a "sub activity"
that you can reuse in different activities).
A fragment must always be embedded in an activity.
Fragment Lifecycle
Fragment in an Activity---Activity Lifecycle influences
Activity paused all its fragments paused
Activity destroyed all its fragments are
destroyed
Activity running manipulate each fragment
independently.
Fragment transaction add, remove, etc.
adds 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.
Fragment Lifecycle
Fragment Lifecycle
• Several callback methods to
handle various stages of a
Fragment lifecycle:
• onCreate() called when
creating the Fragment.
• onCreateView() called
when UI for the Fragment is drawn for the first time.
• onPause() called when the
user is leaving the Fragment.
Fragment inside Activity
It lives in a ViewGroup inside the activity's view
hierarchy
Fragment has its own view layout.
Using Fragments
via XML: Insert a fragment into your activity layout by
declaring the fragment in the activity's layout file, as a
<fragment> element,
via CODE: from your application code by adding it to
an existing ViewGroup.
You may also use a fragment without its own UI as
an invisible worker for the activity.
Fragment methods
(callback functions)
onAttach(Activity) called once the fragment is associated with its activity.
onCreate(Bundle) called to do initial creation of the fragment.
onCreateView(LayoutInflater, ViewGroup, Bundle)creates and returns the view hierarchy associated with the fragment.
onActivityCreated(Bundle) tells the fragment that its activity has completed its own Activity.onCreaate.
onStart() makes the fragment visible to the user (based on its containing activity being started).
onResume() makes the fragment interacting with the user (based on its containing activity being resumed).
Fragment methods
(callback functions)
As a fragment is no longer being used, it goes through a reverse series of callbacks:
onPause() fragment is no longer interacting with the user either because its activity is being paused or a fragment operation is modifying it in the activity.
onStop() fragment is no longer visible to the user either because its activity is being stopped or a fragment operation is modifying it in the activity.
onDestroyView() allows the fragment to clean up resources associated with its View.
onDestroy() called to do final cleanup of the fragment's state.
onDetach() called immediately prior to the fragment no longer being associated with its activity.
Fragments and their UI
Most fragments will have a UI
You must implement the onCreateView() callback
method, which the Android system calls when it's
time for the fragment to draw its layout.
Your implementation of this method must return a
View that is the root of your fragment's layout.
FRAGMENT SUB CLASSES
There are three subclasses of fragments:
DialogFragment : Displays a floating dialog
ListFragment : Displays a list of items that are
managed by an adapter
PreferenceFragment : Displays a hierarchy
of Preference objects as a list
Fragment Application
Using Eclipse, create a new Android project and
name it Fragments.
In the res/layout folder, add a new file and name it
fragment1.xml. Populate it with the following:
<?xml version=”1.0” encoding=”utf-8”?>
<LinearLayout
xmlns:android=”http://schemas.android.com/apk/res/android”
android:orientation=”vertical”
android:layout_width=”fill_parent”
android:layout_height=”fill_parent”
android:background=”#00FF00”>
Fragment Application
<TextView
android:layout_width=”fill_parent”
android:layout_height=”wrap_content”
android:text=”This is fragment #1”
android:textColor=”#000000”
android:textSize=”25sp” />
</LinearLayout>
Fragment Application
Also in the res/layout folder, add another new file and name it fragment2.xml. Populate it as follows:
<?xml version=”1.0” encoding=”utf-8”?>
<LinearLayout
xmlns:android=”http://schemas.android.com/apk/res/android”
android:orientation=”vertical”
android:layout_width=”fill_parent”
android:layout_height=”fill_parent”
android:background=”#FFFE00”>
Fragment Application
<TextView
android:layout_width=”fill_parent”
android:layout_height=”wrap_content”
android:text=”This is fragment #2”
android:textColor=”#000000”
android:textSize=”25sp” />
</LinearLayout>
Fragment Application
In main.xml, add the following code.
<?xml version=”1.0” encoding=”utf-8”?>
<LinearLayout
xmlns:android=”http://schemas.android.com/apk/res/android”
android:layout_width=”fill_parent”
android:layout_height=”fill_parent”
android:orientation=”horizontal” >
<fragment
android:name=”com.example.Fragments.Fragment1”
android:id=”@+id/fragment1”
android:layout_weight=”1”
android:layout_width=”0px”
android:layout_height=”match_parent” />
Fragment Application
<fragment
android:name=”com.example.Fragments.Fragment2”
android:id=”@+id/fragment2”
android:layout_weight=”1”
android:layout_width=”0px”
android:layout_height=”match_parent” />
</LinearLayout>
Fragment Application
Add two Java class files and name them Fragment1.java and Fragment2.java
public class Fragment1 extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
//---Inflate the layout for this fragment---
return inflater.inflate(
R.layout.fragment1, container, false);
}
}
Fragment Application
public class Fragment2 extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
//---Inflate the layout for this fragment---
return inflater.inflate(
R.layout.fragment2, container, false);
}
}
Fragment Application
return inflater.inflate( R.layout.fragment1, container, false)
Fragment Application
How It Works
A fragment behaves very much like an activity — it has a Java
class and it loads its UI from an XML file.
The Java class for a fragment needs to extend the Fragment
base class.
To draw the UI for a fragment, you override the onCreateView()
method. This method needs to return a View object.
Use a LayoutInflater object to inflate the UI from the specified
XML file.
The container argument refers to the parent ViewGroup, which
is the activity in which you are trying to embed the fragment.
The savedInstanceState argument enables you to restore the
fragment to its previously saved state.
Note that each fragment needs a unique identifier. You can
assign one via the android:id or android:tag attribute.
Adding Fragments
Dynamically
Fragments enable to compartmentalize UI into various
configurable parts.
The real power of fragments is realized when you add them
dynamically to activities during runtime.
It is much more useful to create fragments and add them to
activities during runtime.
This enables you to create a customizable user interface for
your application.
For example, if the application is running on a smartphone, you
might fill an activity with a single fragment; if the application is
running on a tablet, you might then fill the activity with two or
more fragments, as the tablet has much more screen real
estate compared to a smartphone.
Adding Fragments
DynamicallyUsing the same project created in the previous section, modify the main.xml file by
commenting out the two <fragment> elements:
<!--
<fragment
android:name=”com.example.Fragments.Fragment1”
……..
android:layout_height=”match_parent” />
<fragment
android:name=”com.example.Fragments.Fragment2”
……
android:layout_height=”match_parent” />
-->
Adding Fragments
DynamicallyAdd the following code in bold to the FragmentsActivity.java file.
public class FragmentsActivity extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
android.app.FragmentManager fragmentManager =
getFragmentManager();
FragmentTransaction fragmentTransaction =
fragmentManager.beginTransaction();
WindowManager wm = getWindowManager();
Display d = wm.getDefaultDisplay();
Adding Fragments
Dynamically
Point size = new Point();
d.getSize(size);
int width = size.x;
int height = size.y;
if (width > height)
{
//---landscape mode---
Fragment1 fragment1 = new Fragment1();
// android.R.id.content refers to the content
// view of the activity
fragmentTransaction.replace(android.R.id.content, fragment1);
}
Adding Fragments
Dynamically
else
{
//---portrait mode---
Fragment2 fragment2 = new Fragment2();
fragmentTransaction.replace(android.R.id.content,
fragment2);
}
fragmentTransaction.commit();
}
}
Adding Fragments
Dynamically
THE END