Android Fragment Lifecycle Explained – Complete Guide with Real Examples (Kotlin & Java)

Learn Android Fragment Lifecycle in detail with real examples. Understand onCreateView, onViewCreated, onStart, onResume and more in Kotlin and Java.

Android Fragment Lifecycle Explained – Complete Guide with Real Examples (Kotlin & Java)

Introduction

After understanding Activity Lifecycle, the next important concept is Fragment Lifecycle.

If Activity is a full screen, then Fragment is a section of that screen.

For example:

  • WhatsApp → Chat screen = Activity

  • Chat list tab = Fragment

  • Status tab = Fragment

  • Calls tab = Fragment

Fragments allow us to create modular, reusable UI components.

But like Activities, Fragments also go through lifecycle states. And this lifecycle is slightly more complex.

Understanding Fragment lifecycle properly is essential when building:

  • Tab layouts

  • Dashboard screens

  • POS dashboards

  • ViewPager screens

  • Multi-pane layouts

Let’s break it down clearly.

What is a Fragment?

A Fragment is a reusable UI component that must live inside an Activity.

Important point:

👉 Fragment cannot exist without an Activity.
👉 Activity can exist without a Fragment.

Fragment lifecycle is closely connected to Activity lifecycle.

Complete Fragment Lifecycle Methods

Main methods:

onAttach()
onCreate()
onCreateView()
onViewCreated()
onStart()
onResume()
onPause()
onStop()
onDestroyView()
onDestroy()
onDetach()

It looks complex, but we will simplify it.

Fragment Lifecycle Flow (Simple View)

When Fragment is added:

onAttach() onCreate() onCreateView() onViewCreated() onStart() onResume()

When Fragment is removed:

onPause()
onStop()
onDestroyView()
onDestroy()
onDetach()

1. onAttach() – Fragment Attached to Activity

Called when Fragment connects to Activity.

Kotlin:

override fun onAttach(context: Context) { super.onAttach(context) println("Fragment attached") }

Used for:

  • Getting Activity reference

  • Initial setup

2. onCreate() – Fragment Created

Similar to Activity’s onCreate, but UI is NOT created here.

Used for:

  • Initializing variables

  • Preparing data

3. onCreateView() – UI is Created (Most Important)

This is where Fragment layout is inflated.

Kotlin:

override fun onCreateView( inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle? ): View? { return inflater.inflate(R.layout.fragment_home, container, false) }

Java:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    return inflater.inflate(R.layout.fragment_home, container, false);
}

This method creates the UI.

Very important.

4. onViewCreated() – UI Ready

Called after onCreateView.

Used for:

  • Setting click listeners

  • Initializing RecyclerView

  • Binding data

Kotlin:

override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) println("View created") }

Professional developers usually handle UI setup here.

5. onStart() and onResume()

Same meaning as Activity:

  • onStart → Fragment visible

  • onResume → User interacting

Real Example – Tab Layout Scenario

Imagine dashboard with 3 tabs:

  • Sales

  • Products

  • Customers

When you switch tab:

Old fragment:

onPause() onStop()

New fragment:

onStart()
onResume()

If fragment is removed completely:

onDestroyView() onDestroy() onDetach()

Important Difference: onDestroyView() vs onDestroy()

This is where many beginners get confused.

onDestroyView()

Called when UI is destroyed but Fragment object still exists.

Example:

  • Fragment in ViewPager

  • Fragment replaced

Use this to:

  • Clear view binding

  • Release UI references

onDestroy()

Called when Fragment instance is fully destroyed.

Use this to:

  • Cleanup data

  • Remove listeners

Why Fragment Lifecycle is More Complex?

Because Fragment has two lifecycles:

  1. Fragment lifecycle

  2. Fragment View lifecycle

The View lifecycle starts in:

onCreateView()

And ends in:

onDestroyView()

This separation prevents memory leaks.

Real Production Example (POS Dashboard)

Imagine POS app dashboard:

Activity → DashboardActivity

Fragments:

  • SalesFragment

  • StockFragment

  • ReportsFragment

When switching fragments:

Only fragment lifecycle changes, Activity stays alive.

This makes app more efficient.

Common Beginner Mistakes

❌ Initializing UI in onCreate()
❌ Not clearing binding in onDestroyView()
❌ Loading heavy data in onResume repeatedly

Correct approach:

  • Inflate UI in onCreateView

  • Setup UI in onViewCreated

  • Clear binding in onDestroyView

Fragment vs Activity Lifecycle (Quick Comparison)

Feature Activity Fragment
Independent Yes No
Needs container No Yes
Has onCreateView No Yes
Has onDestroyView No Yes
Reusable UI No Yes

When Should You Use Fragments?

Use Fragment when:

  • You need reusable UI sections

  • You use tab layout

  • You use ViewPager

  • You want modular design

  • You build scalable apps

Modern Android apps heavily use Fragments.

Conclusion

Fragment Lifecycle may look complicated initially, but once you understand the separation between:

  • Fragment lifecycle

  • View lifecycle

Everything becomes logical.

Fragments are powerful tools for building scalable and modular Android applications.

If you master Activity and Fragment lifecycle properly, you are already ahead of many beginners.

Share

What's Your Reaction?

Like Like 0
Dislike Dislike 0
Love Love 0
Funny Funny 0
Angry Angry 0
Sad Sad 0
Wow Wow 0