0% found this document useful (0 votes)
34 views23 pages

Lecture 5 - Fragments

Uploaded by

Night Cloud
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views23 pages

Lecture 5 - Fragments

Uploaded by

Night Cloud
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

Mobile Applications Development

University of Science and Technology


Faculty of Computer Science & Information Technology
Department of Computer science and Information and Communication Technology

Lecture (5)

Fragments Mr. Mohanad Shahata

This content has been cited from Mr. Kirolos Tharwat’s content.
Introduction to Fragment

Fragments:
 Sub-activities, each with its own set of views.

 One or more fragments can be embedded in an Activity.

 You can use a fragment across multiple activities.

 Fragments were added with the release of Android 3.0, (API level 11).

 Fragments make an application respond faster and provide a more continuous


user experience.

2
Introduction to Activities

 Fragments can be used dynamically as a function of the device type


(tablet or not) or orientation.

3
Single-pane & Multi-pane layouts

• A panel or pane represents a part of the user interface.


• The term pane is a general term used to describe the concept that
multiple views are combined into one compound view depending on
the actual available space.

4
Single-pane & Multi-pane layouts Cont.

If not enough space is available only one panel is shown. This is


typically called a single-pane layout.

If more space is available, multiple panels can be shown.

5
Single-pane & Multi-pane layouts Cont.

6
Fragment

A Fragment represents a behaviour 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.

7
Expert Android Studio by Murat Yener & Onur Dundar

Fragment Lifecycle

8
Fragment methods (callback functions)

• onAttach() called once the fragment is associated with its activity.

• onCreate() called to do initial creation of the fragment.

• onCreateView() creates and returns the view hierarchy associated with the
fragment.

• onActivityCreated() tells the fragment that its activity has completed its own
Activity.onCreate().

• onStart() makes the fragment visible to the user.

• onResume() makes the fragment interacting with the user.

9
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.

10
Fragment Lifecycle

Fragment in an Activity  Activity Lifecyle influences


• Activity running  manipulate each fragment independently.
• Activity paused  all its fragments paused.
• Activity destroyed  all its fragments are destroyed.

Fragment transaction  add, remove, replace.


• 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.

11
Fragment Lifecycle

12
How to use Fragments

1- Create a layout xml file for Fragment fragment_sample.xml.

<?xml version="1.0" encoding="utf-8"?>


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

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">

<fragment android:name="com.example.SampleFragment "


android:id="@+id/list"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent" />
</LinearLayout>

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

FragmentManager fragmentManager = getFragmentManager();


FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

//Create instance of your Fragment


SampleFragment fragment = new SampleFragment ();
//Add Fragment instance to your Activity
fragmentTransaction.add(R.id.fragment_container, fragment);
fragmentTransaction.commit();
activity_main.xml
android:id="@+id/fragment_container"

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);

If you do not call addToBackStack() when you perform a transaction that


removes/replaces a fragment, then that fragment is destroyed when the
transaction is committed and the user cannot navigate back to it.

Whereas, if you do call addToBackStack() when removing/replacing a fragment,


then the fragment is stopped and will be resumed if the user navigates back.
19
Fragment – extend a Fragment class

• Via CODE: extend android.app.Fragment OR one of its subclasses:


 DialogFragment
 ListFragment
 PreferenceFragment
 WebViewFragment

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

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy