---
title: "Fragment Life cycle in Android"  
description: "In this article I am explaining Fragment Life cycle in Android"  
author: "Manoj Pandey"  
published: 2015-03-27  
updated: 2019-09-07  
canonical: https://www.mindstick.com/articles/1694/fragment-life-cycle-in-android  
category: "android"  
tags: ["android", "android fragment"]  
reading_time: 3 minutes  

---

# Fragment Life cycle in Android

[Android](https://www.mindstick.com/articles/1567/navigation-drawer-implementation-in-android) [fragment](https://www.mindstick.com/forum/34147/what-is-fragment-life-cycle-in-android) is a part of an [activity](https://www.mindstick.com/forum/23119/how-to-save-activity-state-in-android). Fragment is a piece of activity for [user interface](https://www.mindstick.com/blog/302016/what-is-user-interface-and-what-are-its-benefits). A single activity can contain [multiple](https://www.mindstick.com/blog/12797/iowa-is-expected-to-see-heavy-growth-in-multiple-sectors) fragments and many fragments can be reused in many, different activities. In Activity we can [add or remove](https://www.mindstick.com/forum/160320/how-you-can-check-if-an-element-exists-in-a-set-and-add-or-remove-elements-from-it) fragment when [application is running](https://answers.mindstick.com/qa/33369/how-will-you-find-if-your-application-is-running-on-low-memory-or-out-of-memory) and a fragment can be used multiple activities.\

##### In activity fragment has own life cycle and very similar to an android activity lifecycle. Core life cycle of fragment

##### 1. onAttach(Activity) called once the fragment is associated with its activity.

##### 2. onCreate(Bundle) called to do initial creation of the fragment.

##### 3. onCreateView(LayoutInflater, ViewGroup, Bundle) creates and returns the view

##### hierarchy associated with the fragment.

##### 4. onActivityCreated(Bundle) tells the fragment that its activity has completed

##### its own Activity.onCreate().

##### 5. onViewStateRestored(Bundle) tells the fragment that all of the saved state of

##### its view hierarchy has been restored.

##### 6. onStart() makes the fragment visible to the user (based on its containing

##### activity being started).

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

![Fragment Life cycle in Android](https://www.mindstick.com/mindstickarticle/4fb8c873-a948-4de1-b759-dac86e675c11/images/43842ab6-0727-48b4-b8ca-320c8cbd70fb.png)

Here I am creating a sample of Fragment life cycle which tell you which method call according to life cycle on using fragment

\
1. create layout file for fragment1.xml

```
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical" >     <TextView        android:id="@+id/textView1"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="my fragment" /> </LinearLayout>
```

2. Add a class which extends with fragment class and paste following code

```
package com.example.demofragment; import android.app.Activity;import android.app.Fragment;import android.os.Bundle;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.Toast; public class MyFragment extends Fragment {      @Override     public void onAttach(Activity activity) {           // TODO Auto-generated method stub           Toast.makeText(getActivity(), "onAttach", 100).show();           super.onAttach(activity);     }      @Override     public View onCreateView(LayoutInflater inflater, ViewGroup container,                Bundle savedInstanceState) {           // TODO Auto-generated method stub           return inflater.inflate(R.layout.fragment1, container, false);     }      @Override     public void onActivityCreated(Bundle savedInstanceState) {           // TODO Auto-generated method stub           Toast.makeText(getActivity(), "onCreate", 100).show();           super.onActivityCreated(savedInstanceState);     }      @Override     public void onCreate(Bundle savedInstanceState) {           // TODO Auto-generated method stub           super.onCreate(savedInstanceState);     }      @Override     public void onDestroy() {           // TODO Auto-generated method stub           Toast.makeText(getActivity(), "onDestroy", 100).show();           super.onDestroy();     }      @Override     public void onDestroyView() {           // TODO Auto-generated method stub           super.onDestroyView();     }      @Override     public void onDetach() {           // TODO Auto-generated method stub           Toast.makeText(getActivity(), "onDetach", 100).show();           super.onDetach();     }      @Override     public void onPause() {           // TODO Auto-generated method stub           Toast.makeText(getActivity(), "onPause", 100).show();           super.onPause();     }      @Override     public void onResume() {           // TODO Auto-generated method stub           super.onResume();     }      @Override     public void onStart() {           // TODO Auto-generated method stub           super.onStart();     }      @Override     public void onStop() {           // TODO Auto-generated method stub           super.onStop();     } } 
```

3. Add fragment in activity_main.xml file

```
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:paddingBottom="@dimen/activity_vertical_margin"    android:paddingLeft="@dimen/activity_horizontal_margin"    android:paddingRight="@dimen/activity_horizontal_margin"    android:paddingTop="@dimen/activity_vertical_margin"    tools:context=".MainActivity" >     <fragment        android:id="@+id/myfragment"        android:name="com.example.demofragment.MyFragment"        android:layout_width="wrap_content"        android:layout_height="wrap_content" /> </RelativeLayout>
```

4. No need to add extra code in MainActivity class

```
package com.example.demofragment; import android.os.Bundle;import android.app.Activity;import android.view.Menu; public class MainActivity extends Activity {                 @Override                protected void onCreate(Bundle savedInstanceState) {                                super.onCreate(savedInstanceState);                                setContentView(R.layout.activity_main);                }                 }
```

Now run your application

![Fragment Life cycle in Android](https://www.mindstick.com/mindstickarticle/4fb8c873-a948-4de1-b759-dac86e675c11/images/f4de9f5f-c1f1-4cd8-b453-eb7256df2c09.png) ![Fragment Life cycle in Android](https://www.mindstick.com/mindstickarticle/4fb8c873-a948-4de1-b759-dac86e675c11/images/42ce7868-4051-4504-8186-0011f71799f0.png)

---

Original Source: https://www.mindstick.com/articles/1694/fragment-life-cycle-in-android

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
