Overview
I hope you had a chance to read my last articles, in which I explained about adding a fragment statically and dynamically in an activity. Here are some useful links,
- Android Programming - Day One
- Android Programming - Day Two
- How To Display A Dialog Windows In Android
- How To Display A Progress Dialog Window In Android
- Intent In Android
- Return Data Using Intent Object In Android Applications
- Passing Data Using An Intent Object In Android Applications
- Fragments In Android Applications
- Adding Fragments Dynamically In Android Application
Introduction
- package com.example.administrator.myfragmentapp;
- import android.app.Activity;
- import android.app.Fragment;
- import android.os.Bundle;
- import android.util.Log;
- import android.view.LayoutInflater;
- import android.view.Menu;
- import android.view.MenuItem;
- import android.view.View;
- import android.view.ViewGroup;
- public class Fragment1Activity extends Fragment
- {
- @Override
- public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
- Log.d("Fragment Ist", "onCreateView");
- return inflater.inflate(R.layout.activity_fragment1, container, false);
- }
- @Override
- public void onAttach(Activity activity) {
- super.onAttach(activity);
- Log.d("Fragment Ist", "onAttach");
- }
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- Log.d("Fragment Ist", "onCreate");
- }
- @Override
- public void onActivityCreated(Bundle savedInstanceState) {
- super.onActivityCreated(savedInstanceState);
- Log.d("Fragment Ist", "onActivityCreated");
- }
- @Override
- public void onStart() {
- super.onStart();
- Log.d("Fragment Ist", "onStart");
- }
- @Override
- public void onResume() {
- super.onResume();
- Log.d("Fragment Ist", "onResume");
- }
- @Override
- public void onPause() {
- super.onPause();
- Log.d("Fragment Ist", "onPause");
- }
- @Override
- public void onStop() {
- super.onStop();
- Log.d("Fragment Ist", "onStop");
- }
- @Override
- public void onDestroyView() {
- super.onDestroyView();
- Log.d("Fragment Ist", "onDestroyView");
- }
- @Override
- public void onDestroy() {
- super.onDestroy();
- Log.d("Fragment Ist", "onDestroy");
- }
- public void onDetach() {
- super.onDetach();
- Log.d("Fragment Ist", "onDetach");
- }
- }



- onAttach()
- onCreate()
- onCreateView()
- onActivityCreated()
When the fragment becomes visible, it goes through these states:
- onStart()
- onResume()
When the fragment goes into the background mode, it goes through these states:
- onPause()
- onStop()
When the fragment is destroyed, it goes through these states:
- onPause()
- onStop()
- onDestroyView()
- onDestroy()
- onDetach()
Just like an activity, you can restore an instance of a fragment, using a Bundle object, in the following states:
- onCreate()
- onCreateView()
- onActivityCreated()
Here, we see that most of the states experienced by a fragment are similar to those of activities. Few new states are specific to the fragments, which are:.
- onAttached() – This is called, when the fragment has been associated with the activity.
- onCreateView() – This is called to create the view for the fragment.
- onActivityCreated() – This is called, when the activity’s onCreate() method has been returned.
- onDestroyView() – This is called, when the fragment’s view is being removed.
- onDetach() – This is called when the fragment is detached from the activity.
There is one main difference between the activity and the fragment. When an activity goes into the background, the activity is placed in the back stack. This allows the activity to be returned when the user clicks the Back button, but in the case of fragments, they are not automatically placed in the back stack when they go into the background. For this, you need to call explicitly the addToBackStack() method during a fragment transaction.
In the above article, we learned about the life cycle of a fragment. In the next article, I will explain about the interactions between two or more fragments.

Vignesh ManiPosted Aug 2, 2016, 5:40 AM
Nice one
kalu singh raoPosted Aug 2, 2016, 1:38 AM
Nice