Android fragment is more than Android4.0, and fragmentactivity is for compatibility with the fragment of the 4.0 versions below.
So if you want to be compatible with Android version 4.0 below using fragment, the framework activity needs to inherit fragmentactivity,fragmentactivity this class is in ANDROID.SUPPORT.V4.APP.FRAGMENTACT In the ivity.
Here are 2 ways to use:
1. Inherit the activity.
(This only uses fragment for Android platforms over 4.0).
Framework activity:
Package com.tandong.fragment;
Import android.app.Activity;
Import android.app.Fragment;
Import Android.app.FragmentManager;
Import android.app.FragmentTransaction;
Import Android.os.Bundle;
Import Android.view.View;
Import Android.view.View.OnClickListener;
Import Android.widget.Button;
/**
* Incompatible with 4.0 below mode fragment
*
* @author Tandong
*
*/
public class Mt_activity extends Activity implements Onclicklistener {
Private Button Btn_first, Btn_second;
Private Fragment Fragment_first, Fragment_second;
Private Fragmenttransaction fragmenttransaction;
Private Fragmentmanager Fragmentmanager;
@Override
protected void OnCreate (Bundle savedinstancestate) {
TODO auto-generated Method Stub
Super.oncreate (savedinstancestate);
Setcontentview (r.layout.fragment);
Initview ();
Fragment_second = new Fragment_second ();
Fragmentmanager = This.getfragmentmanager ();
Fragmenttransaction = Fragmentmanager.begintransaction ();
Fragmenttransaction.replace (R.id.main_fragment_layout, Fragment_second, "second_fragment");
Fragmenttransaction.commit ();
}
private void Initview () {
Btn_first = (Button) This.findviewbyid (R.id.btn_first);
Btn_second = (Button) This.findviewbyid (R.id.btn_second);
Btn_first.setonclicklistener (this);
Btn_second.setonclicklistener (this);
}
@Override
public void OnClick (View arg0) {
Switch (Arg0.getid ()) {
Case R.id.btn_first:
Load a different fragment
if (null = = Fragment_first) {
Fragment_first = new Fragment_first ();
}
Fragmenttransaction = Fragmentmanager.begintransaction ();
Fragmenttransaction.replace (R.id.main_fragment_layout, Fragment_first, "fist_fragment");
Fragmenttransaction.commit ();
Break
Case R.id.btn_second:
if (null = = Fragment_first) {
Fragment_second = new Fragment_second ();
}
Fragmenttransaction = Fragmentmanager.begintransaction ();
Fragmenttransaction.replace (R.id.main_fragment_layout, Fragment_second, "second_fragment");
Fragmenttransaction.commit ();
Break
Default
Break
}
}
}
Fragment Code:
Package com.tandong.fragment;
Import android.app.Fragment;
Import Android.os.Bundle;
Import Android.view.LayoutInflater;
Import Android.view.View;
Import Android.view.ViewGroup;
public class Fragment_second extends Fragment {
Private View Rootview;
@Override
Public View Oncreateview (layoutinflater inflater, ViewGroup container, Bundle savedinstancestate) {
if (container==null)
return null;
Rootview = Inflater.inflate (R.layout.fragment_two, Container,false);
return rootview;
}
}
2. Inherit fragmentactivity (backwards compatible with version 4.0, use fragment, import the contents of the ANDROID.SUPPORT.V4 package)
Framework activity:
Package com.tandong.fragment;
Import Android.os.Bundle;
Import android.support.v4.app.Fragment;
Import android.support.v4.app.FragmentActivity;
Import Android.support.v4.app.FragmentManager;
Import android.support.v4.app.FragmentTransaction;
Import Android.view.View;
Import Android.view.View.OnClickListener;
Import Android.widget.Button;
/**
* Compatible with 4.0 following modes fragment
*
* @author Tandong
*
*/
public class Mt_activity extends Fragmentactivity implements Onclicklistener {
Private Button Btn_first, Btn_second;
Private Fragment Fragment_first, Fragment_second;
Private Fragmenttransaction fragmenttransaction;
Private Fragmentmanager Fragmentmanager;
@Override
protected void OnCreate (Bundle savedinstancestate) {
TODO auto-generated Method Stub
Super.oncreate (savedinstancestate);
Setcontentview (r.layout.fragment);
Initview ();
Fragment_first = new Fragment_first ();
Fragmentmanager = This.getsupportfragmentmanager ();
Fragmenttransaction = Fragmentmanager.begintransaction ();
Fragmenttransaction.replace (R.id.main_fragment_layout, Fragment_first, "first_fragment");
Fragmenttransaction.commit ();
}
private void Initview () {
Btn_first = (Button) This.findviewbyid (R.id.btn_first);
Btn_second = (Button) This.findviewbyid (R.id.btn_second);
Btn_first.setonclicklistener (this);
Btn_second.setonclicklistener (this);
}
@Override
public void OnClick (View arg0) {
Switch (Arg0.getid ()) {
Case R.id.btn_first:
Load a different fragment
if (null = = Fragment_first) {
Fragment_first = new Fragment_first ();
}
Fragmenttransaction = Fragmentmanager.begintransaction ();
Fragmenttransaction.replace (R.id.main_fragment_layout, Fragment_first, "fist_fragment");
Fragmenttransaction.commit ();
Break
Case R.id.btn_second:
if (null = = Fragment_first) {
Fragment_second = new Fragment_second ();
}
Fragmenttransaction = Fragmentmanager.begintransaction ();
Fragmenttransaction.replace (R.id.main_fragment_layout, Fragment_second, "second_fragment");
Fragmenttransaction.commit ();
Break
Default
Break
}
}
}
Fragment Code:
Package com.tandong.fragment;
Import Android.os.Bundle;
Import android.support.v4.app.Fragment;
Import Android.view.LayoutInflater;
Import Android.view.View;
Import Android.view.ViewGroup;
public class Fragment_first extends Fragment {
Private View Rootview;
@Override
Public View Oncreateview (layoutinflater inflater, ViewGroup container, Bundle savedinstancestate) {
if (container==null)
return null;
Rootview = Inflater.inflate (R.layout.fragment_first, Container,false);
return rootview;
}
}
Finally, a sentence layout:
<relativelayout xmlns:android= "Http://schemas.android.com/apk/res/android"
Android:layout_width= "Match_parent"
android:layout_height= "Match_parent" >
<linearlayout
Android:id= "@+id/top_bar_layout"
Android:layout_width= "Match_parent"
android:layout_height= "Wrap_content"
Android:layout_alignparenttop= "true"
android:orientation= "Horizontal" >
<button
Android:id= "@+id/btn_one"
Android:layout_width= "Match_parent"
android:layout_height= "Wrap_content"
android:layout_weight= "1"
android:background= "@drawable/TOP_BAR_BG"
android:text= "button One"/>
<button
Android:id= "@+id/btn_two"
Android:layout_width= "Match_parent"
android:layout_height= "Wrap_content"
android:layout_weight= "1"
android:background= "@drawable/TOP_BAR_BG"
android:text= "button Two"/>
</LinearLayout>
<linearlayout
Android:id= "@+id/fragment_replace_layout"
Android:layout_width= "Match_parent"
android:layout_height= "Match_parent"
android:layout_below= "@id/top_bar_layout"
android:background= "#ff0000" >
</LinearLayout>
</RelativeLayout>
Layouts like this can be, not necessarily framelayout
Android fragment and fragmentactivity differences and usage