Fragment allows the activity to be split into multiple reusable controls that are completely individually encapsulated. Each component has its own life cycle and UI layout. (similar to an activity embedded in the activity)
By inheriting fragment to create a new fragment, in most cases you need to assign a UI (XML layout file) for fragment, or you can create a fragment that does not have any background behavior for the UI.
public class Myfragment extends fragment{
Call this method to connect its parent activity
@Override
public void Onattach (activity activity) {
Super.onattach (Activity);
Get a reference to the parent activity
}
Call this method to make the initial creation of the fragment
@Override
public void OnCreate (Bundle saveinstancestate) {
Super.oncreate (saveinstancestate);
Initialize Fragment
}
When fragment is created, the method is called when it continues to create its own user interface
@Override
public void Oncreateview (Layoutinflater inflater,
ViewGroup container, Bundle savedinstancestate) {
Creates or populates a fragment UI and returns it without the UI returning NULL
Return Inflater.inflate (R.layout.head_fragment_viead, container, false);
}
This method is called once the UI of the parent activity and fragment has been created.
@Override
public void onactivitycreated (Bundle savedinstancestate) {
Super.onactivitycreated (savedinstancestate);
Completion of initialization---especially when the parent activity is initialized or the fragment view is fully populated
Before you can do it.
}
Called at the beginning of the visible life cycle
@Override
public void OnStart () {
Super.onstart ();
Apply all required UI changes, now fragment is visible
}
Called at the beginning of the activity life cycle
@Override
public void Onresume () {
Super.onresume ();
Restores all paused fragment required UI updates, threads, or processes. But it's paused in the inactive state.
}
Called at the end of the activity life cycle
@Override
public void OnPause () {
When fragment is not the active foreground fragment, it is necessary to pause the UI updates, suspend the thread, or pause the centralized processing of those CPUs that do not need to be updated. Since this method is called, the process may be terminated, so all editing and state change information is saved
Super.onpause ();
}
Call this method to save the state change of the UI when the activity life cycle ends
@Override
public void Onsaveinstancestate (Bundle savedinstancestate) {
Save the UI's state change information to Savedinstancestate
Super.onsaveinstancestate (savedinstancestate);
}
Called at the end of the visible life cycle
@Override
public void OnStop () {
Pauses the rest of the UI update, suspends the thread, or pauses processing that is no longer needed when the fragment is not visible
Super.onstop ();
}
When the fragment view is detached, the method is called
@Override
public void Ondestroyview () {
Clear resource-related view
Super.ondestroyview ();
}
Call this method at the end of the entire life cycle
@Override
public void OnDestroy () {
Clear all resources, including end thread and close database connection, etc.
Super.ondestroy ();
}
When fragment is detached from its parent activity, the method is called
@Override
public void Ondetach () {
Super.ondetach ();
}
}
The fragment is populated in a static manner into the activity.
<fragment
Android:name= "package. Myfragment "
/>
Dynamically populate the activity with fragment.
The fragment state transition is closely related to the state transition of his corresponding activity. When fragment pauses or stops, it is important to save all of the UI state and persisted data.
Each activity contains a fragment manager to manage the fragment it contains.
Get Fragmentmanager
Fragmentmanager Fragmentmanager = Getfragmentmanager ();
Get Fragmenttransaction
Fragmenttransaction fragmenttransaction = Fragmentmanager.begintransaction ();
Add, delete, replace, specify animation, add to back stack.
Add to
Fragmenttransaction.add (R.id.title_fragment,new myfragment ());
Find, delete
Fragment Fragment = Fragmentmanager.findfragmentbyid (r.id.title_fragment)
Fragmenttransaction.remove (fragment);
Replace
Fragmenttransaction.replace (r.id.body_fragment,new bodyfragment (index))
Set back Stack
String tag = null;
Fragmenttransaction.addtobackstack (tag);
Add an animation to receive two animated XML
Fragmenttransaction.setcustomanimations (R.animator.silde_in_left,
R.animator.silde_out_right);
Fragmenttransaction.commit ();
? communicate to activity through the callback interface. Where fragment needs to share events with its main activity, it is a good idea to create a callback interface in fragment, and the main activity must implement it.
Private MyListener MyListener;
Private String str;
public interface mylistener{
public void Getstr (String str);
}
public void Onattach (activity activity) {
Super.onattach (activity);
Catch exception
MyListener = (mylistener) activity;
str = new String ("callback");
Mylistener.getstr (str);
}
There are occasions when the fragment of the user interface is appropriate for background tasks that interact regularly with the UI or when activity restarts due to configuration changes, saving state becomes particularly important.
FragmentManager.beginTransaction.add (Workerfragment,tag). commit;
Myfragment = (myfragment) fragmentmanager.findfragmentbytag (TAG);
Easy-to-use fragment subclasses. Dialogfragment,listfragment,webviewfragment.
Beginner _ANDROID4 Advanced Programming-2 Fragment