Sunday 19 August 2012

Creating Fragments in the simplest possible way-1


What are fragments?


Fragments in Android are like modular sections of a single activity having their own life-cycle.Umm..sort of sub-activity.

Fragments life cycle methods are affected by its host(Parent) life cycle.For ex:When parent activity is paused,so are all the fragments.When parent activity is destroyed,so are all the Fragment.When an activity is running,each of the fragments can be manipulated(Well i haven't tried it yet..:P)independently.

You can read more about fragments in 

http://developer.android.com/guide/components/fragments.html


Creating fragments

Like Activities,fragments also have their Life cycle callback methods.


To create a fragment you need to override at least its "onCreateView(Bundle)".This method has to return a view and that view is the root of your fragments layout.

Steps to create fragments

Step1:Create a layout XML file(simple_text_fragment.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="TextView" />

    <TextView
    android:id="@+id/textView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Large Text"
    android:textAppearance="?android:attr/textAppearanceLarge" />

</LinearLayout>
---------------------------------------------------------------------------------------------

Step 2:Create a class that extends Fragment and Override its onCreateView method
---------------------------------------------------------------------------------------------
public  class Example_fragment extends Fragment {
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
Log.v("Fragments","onAttach");
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.v("Fragments","onCreate");
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Log.v("Fragments","onCreateView");
                //1st Arg:Fragments Layout
               //2nd Arg:container(parent)
               //3rd Arg:Boolean arg indicating whether the fragment layout should be attached to       the viewGroup(2nd Arg).In our case,it is no as the system itself is doing it.

return inflater.inflate(R.layout.simple_text_fragment,container,false);
}
@Override
public void onStart() {

super.onStart();
Log.v("Fragments","onStart");

}
@Override
public void onResume() {

super.onResume();
Log.v("Fragments","onResume");
}
@Override
public void onPause() {
super.onPause();
Log.v("Fragments","onResume");
}
@Override
public void onStop() {
super.onStop();
Log.v("Fragments","onResume");
}
@Override
public void onDestroyView() {
super.onDestroyView();
}
@Override
public void onDestroy() {
super.onDestroy();
}
@Override
public void onDetach() {
super.onDetach();
}
}
---------------------------------------------------------------------------------------------
Step 3:
In you parent layout's XML file include the below snippet 
---------------------------------------------------------------------------------------------

    <fragment 
    android:name="com.example.apidemo.Example_fragment"
    android:id="@+id/myfragment"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
    />

---------------------------------------------------------------------------------------------



Step 4:

Create Root Activity that inflates parents layout XML.

That's it.Your activity should  look something similar like this


Happy Coding,
-Rajeev










No comments:

Post a Comment

Note: only a member of this blog may post a comment.