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
In this article, you will learn about the life cycle of a fragment in Android Applications.
Fragments have their own life cycle like an activity. Life cycle of a fragment enables you to properly save an instance of the fragment when it is destroyed and restore it to its previous state, when it is recreated.
Code
- 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");
- }
- }
Result
Run the Application by clicking the buttons Shift + F10. Make sure that the emulator is in landscape mode by clicking the button Ctrl + F11. When the Application is loaded on the Emulator, the following is displayed in the LogCat Window:
Next, change the orientation of the fragment from the landscape to the portrait, the following is displayed in LogCat Window and destroys the current fragment:
Recreate the fragment by calling again onAttach(), onCreate() etc. methods.
Explanation
Fragments have their own life cycle like activities. When a fragment is being created, it goes through the following states:
- onAttach()
- onCreate()
- onCreateView()
- onActivityCreated()
When the fragment becomes visible, it goes through these states:
When the fragment goes into the background mode, it goes through these states:
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.
Conclusion
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.