Android Fragment First

Hi, do you use
I’m Android Developer from version 1.6
The first impact is coming version 3.0, it is Fragment.

Support API


But, we have compatible API in 2.3(11)
android.support.v4.app.Fragment, android.support.v4.app.FragmentActivity

UI Fragment

Decide the place of fragment view in layout.
Manage lifecycle of the fragment.

Add the fragment to activity layout.
Add the fragment in the code of activity

How to use

Create Fragment class(include layout)
Use Fragment in Activity

Sample Fragment

Prepare xml layout(fragment_test.xml)

public class FragmentSample extends Fragment 
{
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }
    
    public View onCreateView(LayoutInflater inflater, ViewGroup parent,
Bundle savedInstanceState) {
   
       View v = inflater.inflate(R.layout.fragment_test, parent, false); 
   
       // View operation is here
   
       return v;
   }
}

View layout should be onCreateView method.

This is use case(API 11).

public class SampleActivity extends FragmentActivity 
{
   
   public void onCreate(Bundle savedInstanceState ) 
   {
       super.onCreate(savedInsntaceState);
	   setContentView(R.layout.activity_main);
	   
	   FragmentManager fm = getSupportedFragmentManager();
	   
	   Fragment fragment = fm.findFragmentById(R.layout.fragment_first);
	   if ( fragment == null ) {
	       fragment = new SampleFragment();
		   fm.beginTransaction().add(R.id.fragment_first, fragment).commit();
	   }
   }
}

Activity use FragementManager.
FragementManager manages Fragment, FragmentTransaction.

Sample Default from Eclipse

The easiest example to use Fragment is eclipse default code. Let’s check it!
This example includes both Activity and Fragment in a same source code.

public class MainActivity extends Activity 
{
        @Override
	protected void onCreate(Bundle savedInstanceState) 
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		if (savedInstanceState == null) 
		{
			getFragmentManager().beginTransaction()
					.add(R.id.container, new PlaceholderFragment()).commit();
		}
	}

        public static class PlaceholderFragment extends Fragment 
	{

		public PlaceholderFragment() 
		{
		}

		@Override
		public View onCreateView(LayoutInflater inflater, ViewGroup container,
				Bundle savedInstanceState) 
		{
			View rootView = inflater.inflate(R.layout.fragment_main, container,
					false);
			return rootView;
		}
	}
}

Layout xml
activity_main.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.atmarkplant.goodtimebiz.MainActivity"
    tools:ignore="MergeRootFrame" />

just only frame

fragment_main.xml

<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="com.atmarkplant.goodtimebiz.MainActivity$PlaceholderFragment" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

</RelativeLayout>

Actual layout is here