Lecture On Fragments
Lecture On Fragments
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.
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.
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>
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;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup
container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_static, container,
false);
}
}
<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>
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.fragment.app.Fragment;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup
container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_dynamic, container,
false);
}
}
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
@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();
}
}
<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" />
<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>
@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:
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?