MAD-lec 11 Android Fragment
MAD-lec 11 Android Fragment
Application Development
Fragment
A Fragment is a piece of an activity which
enable more modular activity design. It will
not be wrong if we say, a fragment is a kind
of sub-activity.
Following are important
points about fragment −
• A fragment has its own layout and its own
behaviour with its own life cycle callbacks.
• You can add or remove fragments in an
activity while the activity is running.
• You can combine multiple fragments in a
single activity to build a multi-pane UI.
• Fragments were added to the Android API
in Honeycomb version of Android which
API version 11.
• A fragment can be used in multiple
activities.
• Fragment life cycle is closely related to the
life cycle of its host activity which means
when the activity is paused, all the
fragments available in the activity will also
be stopped.
• A fragment can implement a behaviour
that has no user interface component.
You create fragments by
extending Fragment class and You can
insert a fragment into your activity layout by
declaring the fragment in the activity's layout
file, as a <fragment> element.
Prior to fragment introduction, we had a
limitation because we can show only a
single activity on the screen at one given
point in time. So we were not able to divide
device screen and control different parts
separately. But with the introduction of
fragment we got more flexibility and
removed the limitation of having a single
activity on the screen at a time.
Now we can have a single activity but each
activity can comprise of multiple fragments
which will have their own layout, events and
complete life cycle.
Following is a typical example of how two UI
modules defined by fragments can be
combined into one activity for a tablet
design, but separated for a handset design.
The application can embed two fragments in
Activity A, when running on a tablet-sized
device. However, on a handset-sized
screen, there's not enough room for both
fragments, so Activity A includes only the
fragment for the list of articles, and when the
user selects an article, it starts Activity B,
which includes the second fragment to read
the article.
How to use Fragments?
• This involves number of simple steps to
create Fragments.
• First of all decide how many fragments
you want to use in an activity. For example
let's we want to use two fragments to
handle landscape and portrait modes of
the device.
• Next based on number of fragments,
create classes which will extend
the Fragment class. The Fragment class
has above mentioned callback functions.
You can override any of the functions
based on your requirements.
• Corresponding to each fragment, you will
need to create layout files in XML file.
These files will have layout for the defined
fragments.
Types of Fragments
• Finally modify activity file to define the
actual logic of replacing fragments based
on your requirement.
• Basically fragments are divided as three
stages as shown below.
• Single frame fragments − Single frame
fragments are using for hand hold devices
like mobiles, here we can show only one
fragment as a view.
• List fragments − fragments having special
list view is called as list fragment
• Fragments transaction − Using with
fragment transaction. we can move one
fragment to another fragment.
Split /Static Fragment
<fragment
android:layout_weight="1"
android:id="@+id/frage1"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
layout_weight
LinearLayout also supports assigning a
weight to individual children with the
android:layout_weight attribute. This
attribute assigns an "importance" value to a
view in terms of how much space it should
occupy on the screen. A larger weight value
allows it to expand to fill any remaining
space in the parent view..
Now Create separate layout and java class
for each fragment
You can remove extra code other than on
create view
public class frag2 extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.fragment_frag2, container, false);
return v;
}
}
Now attach fragment in main
xml java
dynamic fragment in android
In one screen display multiple fragments.
1. Create some fragments
2. Create buttons to display fragments
3. Create a fragment in main xml
<fragment
android:id="@+id/frage1"
android:layout_width="match_parent"
android:layout_height="match_parent"/>