Lecture 5 - Fragments
Lecture 5 - Fragments
Lecture (5)
This content has been cited from Mr. Kirolos Tharwat’s content.
Introduction to Fragment
Fragments:
Sub-activities, each with its own set of views.
Fragments were added with the release of Android 3.0, (API level 11).
2
Introduction to Activities
3
Single-pane & Multi-pane layouts
4
Single-pane & Multi-pane layouts Cont.
5
Single-pane & Multi-pane layouts Cont.
6
Fragment
7
Expert Android Studio by Murat Yener & Onur Dundar
Fragment Lifecycle
8
Fragment methods (callback functions)
• onCreateView() creates and returns the view hierarchy associated with the
fragment.
• onActivityCreated() tells the fragment that its activity has completed its own
Activity.onCreate().
9
Fragment methods (callback functions)
10
Fragment Lifecycle
11
Fragment Lifecycle
12
How to use Fragments
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/fragment_color“ >
…….
……..
</LinearLayout>
13
How to use Fragments
2- Create Fragment SampleFragment.java.
public class SampleFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Set the layout xml file to Fragment
Bundle that provides data
return inflater.inflate(R.layout.fragment_sample, container, false); about the previous instance
of the fragment, if the
} fragment is being resumed
}
LayoutInflater used to inflate the layout, container parameter is the parent ViewGroup (from the activity’s layout) in
which our Fragment layout will be inserted. The savedInstanceState parameter is a Bundle that provides data about the
previous instance of the Fragment.
The inflate() method has three arguments first one is the resource layout which we want to inflate, second is the
ViewGroup to be the parent of the inflated layout. Passing the container is important in order for the system to apply
layout parameters to the root view of the inflated layout, specified by the parent view in which it’s going and the third
parameter is a Boolean value indicating whether the inflated layout should be attached to the ViewGroup (the second
parameter) during inflation.
14
How to use Fragments
15
Fragment inside Activity
3- Add the fragment to the activity.
Via XML
Insert a fragment into your activity layout by declaring the fragment in the activity's
layout file, as a <fragment> element.
<?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">
16
Fragment inside Activity
Via Code
From your application code by adding it to an existing ViewGroup.
/*Inside Activity Code where you want to add Fragment (dynamically anywhere or in onCreate() callback) */
//get FragmentTransaction associated with this Activity
17
Fragment inside Activity
FragmentManager:
The class for interacting with Fragment objects inside an activity.
FragmentTransaction:
The class for performing an atomic set of Fragment operations such as Replace or
Add a Fragment.
18
Example REPLACING fragment dynamically
STEP 1: Create new fragment and create FragementTransaction from Manager.
Fragment newFragment = new ExampleFragment();
FragmentTransaction transaction =getFragmentManager().beginTransaction();
STEP 2: Replace whatever is in the fragment_container view with this fragment.
transaction.replace(R.id.fragment_container, newFragment);
transaction.addToBackStack(null);
STEP 3: Commit the transaction.
addToBackStack (FRAGMENT_NAME);
transaction.commit(); getFragmentMangager().findFragmentByTag(FRAGMENT_NAME);
20
Create your own Fragment class OR use known sub-classes
DialogFragment
• Using this class to create a dialog is a good alternative to using the
dialog helper methods in the Activity class.
21
Create your own Fragment class OR use known sub-classes
PreferenceFragment
Displays a hierarchy of Preference objects as a list, similar to
PreferenceActivity. This is useful when creating a "settings" activity for
your application.
22
Mobile Applications Development
Thanks