The Android SDK 3.0 (Honeycomb) introduced the Fragments API. The Fragments API is basically a set of tools which allows us to create, manage and re-use Chuncks of logic and UI.
For example, let’s assume I’m starting to develop an Application, which should be compatible for both Smartphones and Tablet, as well as support both orientation(landscape, portrait). It’s fairly easy to say that the same “lisviews” or forms will be used on both versions in terms of business logic and behavior with some UI adjustments. the fragments API is exactly what we need in order to do it in an elegant manner.
let’s check out the following sample:
public class MainActivity extends FragmentActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
Button btn = (Button) findViewById(R.id.button);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
FragmentManager fragmentManager = getSupportFragmentManager();
Fragment fragment1= fragmentManager.findFragmentById(R.id.list);
FragmentTransaction fragmentTransatcion
=fragmentManager.beginTransaction();
fragmentTransatcion.hide(fragment1);
fragmentTransatcion.commit();
}
});
}
}
- Our activity extends FragmentActivity (this is available by the support package, which enables us to use the fragments API on 1.6+ devices.
- the layout of the activity holds a single button (with id of “button")
- let’s see what happens in our onClick:
- we get a handle of the SupportFragmentManager
- we find a fragment by id (this is defined be a separate layout file which contains a LinearLayout which has 3 texviews:
- the Fragment1 class is :
- This following code does our action (making the fragment go away from the activity)
<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:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="@string/text_first_fragment_title" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="@string/text_second_fragment_title" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="@string/text_third_fragment_title" />
</LinearLayout>
public class Fragment1 extends Fragment implements OnClickListener{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.frag1,container,false);
return view;
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
}
FragmentTransaction fragmentTransatcion =fragmentManager.beginTransaction();
fragmentTransatcion.hide(fragment1);
fragmentTransatcion.commit();
basically after getting the handle for the fragment manager we can start playing with the fragment (adding new fragments, removing fragment etc..)