Saturday , October 20 2018
Home / Android / Android Fragments

Android Fragments

Android Fragment is the part of activity, it is also known as sub-activity. There can be more than one fragment in an activity. Fragments represent multiple screen inside one activity.It is a piece of an activity which enable more modular activity design.

Features of Fragment:

1) You can add or remove fragments in an activity while the activity is running.
2) A fragment can be used in multiple activities.
3) A fragment can implement a behaviour that has no user interface component.
4) A fragment has its own layout and its own behaviour with its own life cycle callbacks.
5) Fragment life cycle is closely related to the life cycle of its host activity which means when the activity is paused, all the fragments available in the activity will also be stopped.
Prior to fragment introduction, we had a limitation because we can show only a single activity on the screen at one given point in time. So we were not able to divide device screen and control different parts separately. But with the introduction of fragment we got more flexibility and removed the limitation of having a single activity on the screen at a time. Now we can have a single activity but each activity can comprise of multiple fragments which will have their own layout, events and complete life cycle.

Fragment Life Cycle:

No. Method Description
1) onAttach(Activity) it is called only once when it is attached with activity.
2) onCreate(Bundle) It is used to initialize the fragment.
3) onCreateView(LayoutInflater, ViewGroup, Bundle) creates and returns view hierarchy.
4) onActivityCreated(Bundle) It is invoked after the completion of onCreate() method.
5) onViewStateRestored(Bundle) It provides information to the fragment that all the saved state of fragment view hierarchy has been restored.
6) onStart() makes the fragment visible.
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.

 

Android Fragment Example:

1) activity_main.xml

<LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android”
android:layout_width=”fill_parent”
android:layout_height=”fill_parent” >

<fragment
android:id=”@+id/fragment2″
android:name=”com.example.fragmentexample.Fragment2″
android:layout_width=”0px”
android:layout_height=”match_parent”
android:layout_weight=”1″
/>

<fragment
android:id=”@+id/fragment1″
android:name=”com.example.fragmentexample.Fragment1″
android:layout_width=”0px”
android:layout_height=”match_parent”
android:layout_weight=”1″
/>

</LinearLayout>
2) 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”
android:background=”#efb248″
>

<TextView
android:id=”@+id/textView1″
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:text=”fragment frist”
android:textAppearance=”?android:attr/textAppearanceLarge” />

</LinearLayout>

3) fragment2.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”
android:background=”#63e41d”
>

<TextView
android:id=”@+id/textView1″
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:text=”Second Fragment”
android:textAppearance=”?android:attr/textAppearanceLarge” />

</LinearLayout>

4) MainActivity.java

package com.example.fragmentexample;

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);
}
}

5) Fragment1.java

package com.example.fragmentexample;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class Fragment1 extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
return inflater.inflate(R.layout.fragment1,container, false);
}

}

6) Fragment2.java

package com.example.fragmentexample;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class Fragment2 extends Fragment {

public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
return inflater.inflate(R.layout.fragment2,container, false);
}

}

 

7) Preview

fragment12

 

About admin

Check Also

Android Activity Launch Modes

Launch mode is an instruction for Android OS which specifies how the activity should be ...

Leave a Reply