A fragment is a part or a behavior in the interface of an activity. You can combine multiple fragment into one activity to create a faceted interface and you can reuse a fragment in multiple activity. You can think of fragment as a modular activity that has its own life cycle and receives its own events. And can be added or deleted when the activity is executed.
A good post: http://my.oschina.net/u/270164/blog/85347.
Use fragment package as Android.app.FragmentManager , But because it is incompatible with version number 3.0, introduce android.support.v4.app.fragmentmanager Two packages the same definition of fragment is different, so in import, often easy confusion causes the program to error. The following is summed up below:
1, compile error-free, execution times wrong: android.view.InflateException:Binary XML file line #7: Error inflating class fragment
The above error occurs because you have used the <fragment> tag in an. xml file, but the corresponding Xx.java file is android.support.v4.app.Fragment. android.support.v4.app.Fragment: Can be compatible to 1.6 version number, cannot use <fragment> tag, need to add in the program or replace the way to join.
2. Compile Error:
import Android.os.Bundle ; import android.support.v4.app.FragmentActivity ; import Android.support.v4.app.FragmentManager ; import android.support.v4.app.FragmentTransaction ; public class Mainactivity extends fragmentactivity { public static Fragmentmanager CHILDFM; protected void OnCreate (Bundle savedinstancestate) { super. OnCreate (savedinstancestate); Setcontentview (r.layout. Activity_main);CHILDFM =getsupportfragmentmanager (); fragmenttransaction ft= childfm. BeginTransaction (); Frag1 f1= New Frag1 (); Ft. Add (F1, "first"). commit (); }}
code as above. Prompt at add: the method Add (Fragment, String) in Thetype fragmenttransaction are not applicable for the arguments (Frag1, String).
Cause: In my frag1.java. Import packet is android.app.Fragment so error.
Change to import android.support.v4.app.Fragment; just fine.
3, in addition, when the activity is called, the package is android.support.v4.app.FragmentActivity class, all extends is fragmentactivity instead of activity.
Some of the error Android.app.FragmentManager and Android.support.v4.app.FragmentManager bring