0% found this document useful (0 votes)
23 views

Android Fragments

Android Fragments are components of an Activity that represent portions of the user interface, allowing for flexible and adaptable UI designs across different screen sizes. They have their own lifecycle but are dependent on the host Activity's lifecycle, and can be dynamically added, removed, or replaced. There are various types of fragments, including Single Fragment, List Fragment, and Fragment Transaction, each serving different purposes in app development.

Uploaded by

Dipti Ghule
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)
23 views

Android Fragments

Android Fragments are components of an Activity that represent portions of the user interface, allowing for flexible and adaptable UI designs across different screen sizes. They have their own lifecycle but are dependent on the host Activity's lifecycle, and can be dynamically added, removed, or replaced. There are various types of fragments, including Single Fragment, List Fragment, and Fragment Transaction, each serving different purposes in app development.

Uploaded by

Dipti Ghule
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/ 15

1

Android Fragments
In Android, the fragment is the part of Activity which represents a portion of
User Interface(UI) on the screen.

It is the section of the android activity that is very helpful in creating UI


designs that are flexible in nature and auto-adjustable based on the device
screen size.

The UI flexibility on all devices improves the user experience and adaptability
of the application.

Fragments can exist only inside an activity as its lifecycle is dependent on the
lifecycle of host activity.

For example, if the host activity is paused, then all the methods and
operations of the fragment related to that activity will stop functioning, thus
fragment is also termed as sub-activity. Fragments can be added, removed,
or replaced dynamically i.e., while activity is running.

<fragment> tag is used to insert the fragment in an android activity layout. By


dividing the activity’s layout multiple fragments can be added in it.
2

Types of Android Fragments

1. Single Fragment: Display only one single view on the device screen. This
type of fragment is mostly used for mobile phones.
2. List Fragment: This Fragment is used to display a list-view from which the
user can select the desired sub-activity. The menu drawer of apps like
Gmail is the best example of this kind of fragment.
3. Fragment Transaction: This kind of fragments supports the transition
from one fragment to another at run time. Users can switch between
multiple fragments like switching tabs.

Android Fragment Lifecycle


The lifecycle of android fragment is like the activity lifecycle. There are 12 lifecycle methods
for fragment.

Each fragment has it’s own lifecycle but due to the connection with the Activity it
belongs to, the fragment lifecycle is influenced by the activity’s lifecycle.
3
4
5

Methods of the Android Fragment

No. Method Description

1) onAttach(Activity) it is called only once when it is


attached with activity.

2) onCreate(Bundle) It is used to initialize the


fragment.

3) onCreateView(LayoutInflater, creates and returns view


ViewGroup, Bundle) hierarchy.

4) onActivityCreated(Bundle) It is invoked after the completion


of onCreate() method.

5) onViewStateRestored(Bundle) It provides information to the


fragment that all the saved state
of fragment view hierarchy has
been restored.

6) onStart() makes the fragment visible.

7) onResume() makes the fragment interactive.

8) onPause() is called when fragment is no


longer interactive.

9) onStop() is called when fragment is no


longer visible.

10) onDestroyView() allows the fragment to clean up


resources.

11) onDestroy() allows the fragment to do final


clean up of fragment state.

12) onDetach() It is called immediately prior to


the fragment no longer being
associated with its activity.
6

Description
Methods

The very first method to be called when the fragment has been
associated with the activity. This method executes only once
during the lifetime of a fragment.
onAttach() When we attach fragment(child) to Main(parent) activity then it
call first and then not call this method any time(like you run an
app and close and reopen) simple means that this method call
only one time.

This method initializes the fragment by adding all the required


onCreate()
attributes and components.

System calls this method to create the user interface of the


fragment. The root of the fragment’s layout is returned as the
onCreateView() View component by this method to draw the UI.
You should inflate your layout in onCreateView but shouldn’t
initialize other views using findViewById in onCreateView.

It indicates that the activity has been created in which the


onViewCreated() fragment exists. View hierarchy of the fragment also instantiated
before this function call.

The system invokes this method to make the fragment visible on


onStart()
the user’s device.

onResume() This method is called to make the visible fragment interactive.

It indicates that the user is leaving the fragment. System call this
onPause()
method to commit the changes made to the fragment.

Method to terminate the functioning and visibility of fragment from


onStop()
the user’s screen.

System calls this method to clean up all kinds of resources as


well as view hierarchy associated with the fragment. It will call
onDestroyView()
when you can attach new fragment and destroy existing fragment
Resoruce

It is called to perform the final clean up of fragment’s state and its


onDestroy()
lifecycle.

onDetach() The system executes this method to disassociate the fragment


7

Description
Methods

from its host activity.


It will call when your fragment Destroy(app crash or attach new
fragment with existing fragment)

Android Fragment Example


Let's have a look at the simple example of android fragment.

activity_main.xml

File: activity_main.xml
1. <?xml version="1.0" encoding="utf-8"?>
2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3. xmlns:app="http://schemas.android.com/apk/res-auto"
4. xmlns:tools="http://schemas.android.com/tools"
5. android:layout_width="fill_parent"
6. android:layout_height="fill_parent"
7. tools:context="example.javatpoint.com.fragmentexample.MainActivity">
8.
9. <fragment
10. android:id="@+id/fragment1"
11. android:name="example.javatpoint.com.fragmentexample.Fragment1"
12. android:layout_width="0px"
13. android:layout_height="match_parent"
14. android:layout_weight="1"
15. />
16.
17. <fragment
18. android:id="@+id/fragment2"
19. android:name="example.javatpoint.com.fragmentexample.Fragment2"
20. android:layout_width="0px"
21. android:layout_height="match_parent"
22. android:layout_weight="1"
23. />
24.
25. </LinearLayout>
File: fragment_fragment1.xml
8

1. <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
2. xmlns:tools="http://schemas.android.com/tools"
3. android:layout_width="match_parent"
4. android:layout_height="match_parent"
5. android:background="#F5F5DC"
6. tools:context="example.javatpoint.com.fragmentexample.Fragment1">
7.
8. <!-- TODO: Update blank fragment layout -->
9. <TextView
10. android:layout_width="match_parent"
11. android:layout_height="match_parent"
12. android:text="@string/hello_blank_fragment" />
13.
14. </FrameLayout>
File: fragment_fragment2.xml
1. <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
2. xmlns:tools="http://schemas.android.com/tools"
3. android:layout_width="match_parent"
4. android:layout_height="match_parent"
5. android:background="#F0FFFF"
6. tools:context="example.javatpoint.com.fragmentexample.Fragment2">
7.
8. <!-- TODO: Update blank fragment layout -->
9. <TextView
10. android:layout_width="match_parent"
11. android:layout_height="match_parent"
12. android:text="@string/hello_blank_fragment" />
13.
14. </FrameLayout>

MainActivity class

File: MainActivity.java
1. package example.javatpoint.com.fragmentexample;
2.
3. import android.support.v7.app.AppCompatActivity;
4. import android.os.Bundle;
5.
9

6. public class MainActivity extends AppCompatActivity {


7.
8. @Override
9. protected void onCreate(Bundle savedInstanceState) {
10. super.onCreate(savedInstanceState);
11. setContentView(R.layout.activity_main);
12. }
13. }

File: Fragment1.java
1. package example.javatpoint.com.fragmentexample;
2. import android.os.Bundle;
3. import android.support.v4.app.Fragment;
4. import android.view.LayoutInflater;
5. import android.view.View;
6. import android.view.ViewGroup;
7.
8. public class Fragment1 extends Fragment {
9.
10. @Override
11. public void onCreate(Bundle savedInstanceState) {
12. super.onCreate(savedInstanceState);
13. }
14.
15. @Override
16. public View onCreateView(LayoutInflater inflater, ViewGroup container,
17. Bundle savedInstanceState) {
18. // Inflate the layout for this fragment
19. return inflater.inflate(R.layout.fragment_fragment1, container, false);
20. }
21. }

File: Fragment2.java
1. package example.javatpoint.com.fragmentexample;
2. import android.os.Bundle;
3. import android.support.v4.app.Fragment;
4. import android.view.LayoutInflater;
5. import android.view.View;
6. import android.view.ViewGroup;
10

7. public class Fragment2 extends Fragment {


8. @Override
9. public void onCreate(Bundle savedInstanceState) {
10. super.onCreate(savedInstanceState);
11. }
12. @Override
13. public View onCreateView(LayoutInflater inflater, ViewGroup container,
14. Bundle savedInstanceState) {
15. // Inflate the layout for this fragment
16. return inflater.inflate(R.layout.fragment_fragment2, container, false);
17. }
18.
19. }

Output:
11

How to Create a New Fragment in Android


Studio?

Android Studio is the official integrated development environment for
Google’s Android operating system, built on JetBrains’ IntelliJ IDEA
software and designed specifically for Android development.
You can Develop Android App using this. Here, We are going to learn how
to create a new Fragment in Android Studio.
A Fragment is a piece of an activity that enables a more modular activity
design.
A fragment is easier to reuse within activities and layouts. Android devices
have a variety of screen sizes and densities.
It simplifies the reuse of components in different layouts and their logic.
We can also use fragments also to support different layouts for landscape
and portrait orientation on a smartphone.
Step by Step Implementation

Step 1: Create a New Project in Android Studio


To create a new project in Android Studio please refer
to How to Create/Start a New Project in Android Studio.
The code for that has been given in both Java and Kotlin
Programming Language for Android.
Step 2: Create New Fragment
Right-Click on the First button inside Java, then click
on New.
12

Then Go to Fragment
13

The next step is to choose the Fragment type.


Fragment in Android refers to a single screen with a user interface.
For Beginners, “Blank Fragment” is recommended).
Click on Fragment(Blank).
14

Now, A new Dialog Box will appear.


Then, fill in the Fragment Name text field.
In Android studio, files named in CamelCase are preferred.
An XML file is used to provide functionalities of the user
interface for our app.
An XML file of the fragment is created using the first word in
the fragment name.
Click on Finish. And you have created your Fragment
15

You might also like