0% found this document useful (0 votes)
29 views6 pages

Lecture On Fragments

The document provides an overview of Fragments in Android, highlighting their modular nature, lifecycle, and types (static and dynamic). It includes examples of implementing both static and dynamic fragments, as well as methods for fragment transactions and communication between fragments and activities. Additionally, it poses practice questions to reinforce understanding of the concepts discussed.

Uploaded by

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

Lecture On Fragments

The document provides an overview of Fragments in Android, highlighting their modular nature, lifecycle, and types (static and dynamic). It includes examples of implementing both static and dynamic fragments, as well as methods for fragment transactions and communication between fragments and activities. Additionally, it poses practice questions to reinforce understanding of the concepts discussed.

Uploaded by

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

Lecture: Fragments in Android

Introduction to Fragments
In Android development, a Fragment represents a modular portion of an Activity. Fragments
allow for building dynamic and flexible UI designs for both small and large screen devices.

Key Characteristics of Fragments


 Lifecycle: Fragments have their own lifecycle, closely tied to the lifecycle of the hosting
Activity.
 Reusable: They can be reused in multiple Activities.
 Modular: They allow developers to divide the UI and logic of an app into smaller,
manageable components.

Lifecycle of a Fragment
Key Lifecycle Methods
1. onAttach(Context context): Called when the fragment is attached to an activity.
2. onCreate(Bundle savedInstanceState): Initializes fragment-related resources.
3. onCreateView(LayoutInflater inflater, ViewGroup container, Bundle
savedInstanceState): Inflates the fragment's UI.
4. onActivityCreated(Bundle savedInstanceState): Called after the hosting
activity's onCreate method is complete.
5. onStart(): Makes the fragment visible.
6. onResume(): Fragment is active and ready for user interaction.
7. onPause(): Called when the fragment is no longer interacting with the user.
8. onStop(): Fragment is no longer visible.
9. onDestroyView(): Cleans up the view hierarchy.
10. onDetach(): Fragment is detached from the activity.

Types of Fragments
1. Static Fragments: Defined in the XML layout.
2. Dynamic Fragments: Added programmatically at runtime.

Implementing Static and Dynamic Fragments


1. Static Fragment Example
Static fragments are declared in the activity's XML layout.
XML Layout (activity_main.xml)
<LinearLayout

xmlns:android="http://schemas.android.com/apk/res/androi
d"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">

<fragment
android:id="@+id/fragment_static"
android:name="com.example.myapp.StaticFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>

Static Fragment Class (StaticFragment.java)

package com.example.myapp;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.fragment.app.Fragment;

public class StaticFragment extends Fragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup
container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_static, container,
false);
}
}

Fragment Layout (fragment_static.xml)

<LinearLayout

xmlns:android="http://schemas.android.com/apk/res/androi
d"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="This is a Static Fragment" />
</LinearLayout>

2. Dynamic Fragment Example


Dynamic fragments are added programmatically using the FragmentManager.

Dynamic Fragment Class (DynamicFragment.java)


package com.example.myapp;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.fragment.app.Fragment;

public class DynamicFragment extends Fragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup
container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_dynamic, container,
false);
}
}

Adding Dynamic Fragment (MainActivity.java)


package com.example.myapp;

import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Dynamically add fragment
Fragment dynamicFragment = new DynamicFragment();
FragmentManager fragmentManager =
getSupportFragmentManager();
FragmentTransaction fragmentTransaction =
fragmentManager.beginTransaction();
fragmentTransaction.add(R.id.fragment_container,
dynamicFragment);
fragmentTransaction.commit();
}
}

XML Layout (activity_main.xml)

<FrameLayout

xmlns:android="http://schemas.android.com/apk/res/androi
d"
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />

Dynamic Fragment Layout (fragment_dynamic.xml)

<LinearLayout

xmlns:android="http://schemas.android.com/apk/res/androi
d"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="This is a Dynamic Fragment" />
</LinearLayout>

Communication Between Fragment and Activity


Using Interfaces
Define Interface in Fragment (DynamicFragment.java)
public interface FragmentListener {
void onFragmentInteraction(String
data);
}

Implement Interface in Activity (MainActivity.java)


public class MainActivity extends AppCompatActivity implements
DynamicFragment.FragmentListener {

@Override
public void onFragmentInteraction(String data) {
Toast.makeText(this, "Data from Fragment: " + data,
Toast.LENGTH_SHORT).show();
}
}

Fragment Transactions
Key Methods:
 add(): Adds a fragment.
 replace(): Replaces an existing fragment.
 remove(): Removes a fragment.
 addToBackStack(): Adds the transaction to the back stack.

Example:

FragmentManager fragmentManager = getSupportFragmentManager();


FragmentTransaction transaction =
fragmentManager.beginTransaction();
transaction.replace(R.id.fragment_container, new
AnotherFragment());
transaction.addToBackStack(null);
transaction.commit();

Practice Questions
1. Explain the difference between static and dynamic fragments.
2. Write a program to replace one fragment with another on a button click.
3. How can you pass data between an Activity and a Fragment?

You might also like