Fragmentmanager
In order to manage fragments in activity, you need to use Fragmentmanager.
To get it, you need to invoke the Getfragmentmanager () method in the activity.
Because the API for Fragmentmanager is introduced in Android 3.0, API level 11, for previous versions, you need to use the fragmentactivity in the Support library, and use the Getsupportfragmentmanager () method.
The work that can be done with Fragmentmanager is:
get the fragment that exists in the activity:
Use the Findfragmentbyid () or Findfragmentbytag () method.
eject the fragment back stack:
Popbackstack (): ejects the last fragment conversion from the back stack. If there is nothing that can be out of the stack, return false.
This function is asynchronous: It joins the request of the pop-up stack into the queue, but the action is not executed until the application returns to the event loop.
add a listener to the back stack:
Addonbackstackchangedlistener ()
Performing Fragment Transactions
When using fragment, you can perform actions such as adding, removing, replacing, and so on, through user interaction.
All these changes constitute a set, which is called a transaction.
You can call the method in Fragmenttransaction to handle this transaction, and you can deposit transaction in the back stack managed by activity so that the user can perform a fragment-change fallback.
You can get an instance of the Fragmenttransaction class like this:
Copy Code code as follows:
Fragmentmanager Fragmentmanager = Getfragmentmanager ();
Fragmenttransaction fragmenttransaction = Fragmentmanager.begintransaction ();
Each transaction is a set of changes that are executed concurrently.
Use the Add (), remove (), replace () method to add all the required changes, and then invoke the commit () method to apply these changes.
Before the commit () method, you can call Addtobackstack () and add this transaction to the back stack, which is managed by the activity, and when the user presses the return key, will return to the last fragment state.
For example, the following code replaces the previous fragment with a new fragment and stores the previous state in the back stack.
Copy Code code as follows:
Create New Fragment and transaction
Fragment newfragment = new Examplefragment ();
Fragmenttransaction transaction = Getfragmentmanager (). BeginTransaction ();
Replace whatever is in the Fragment_container view with this fragment,
and add the transaction to the back stack
Transaction.replace (R.id.fragment_container, newfragment);
Transaction.addtobackstack (NULL);
Commit the transaction
Transaction.commit ();
In this example, the newfragment will replace the fragment in the R.id.fragment_container container, and if not, the new fragment will be added directly.
by calling Addtobackstack (), a series of transformations of commit () are stored in the back stack as a transaction, and the user presses the home key to return to the previous transition state.
When you remove a fragment, if the commit () does not call Addtobackstack (), that fragment will be destroyed, and if Addtobackstack () is invoked, This fragment will be stopped and can be recovered by the return key.
about the Commit () method
Calling the commit () method does not immediately execute the change action contained in transaction, and the Commit () method joins transaction into the UI thread queue of the activity.
However, if you feel it is necessary, you can invoke the Executependingtransactions () method to immediately execute the transaction provided by the commit ().
This is usually not necessary unless the transaction is dependent on other threads.
Note: You can only invoke commit () before the activity stores its state (when the user leaves the activity), and if a commit () is invoked after the store state, an exception is thrown.
This is because the state of the commit will be lost when the activity is restored again. If it's okay to lose, then use the Commitallowingstateloss () method.
Instance Program
wrote a small program to practice the management of fragment, the program is not very perfect, is to try the basic use, first press the first button to add a fragment, the second button to replace it, the third button to add the second button fragment deleted.
Related code:
The first one fragment:
Copy Code code as follows:
Examplefragment.java
Package com.example.learningfragment;
Import Android.os.Bundle;
Import android.support.v4.app.Fragment;
Import Android.view.LayoutInflater;
Import Android.view.View;
Import Android.view.ViewGroup;
public class Examplefragment extends Fragment
{
Three methods that must be overloaded generally
@Override
public void OnCreate (Bundle savedinstancestate)
{
TODO auto-generated Method Stub
Super.oncreate (savedinstancestate);
System.out.println ("Examplefragment--oncreate");
}
@Override
Public View Oncreateview (layoutinflater inflater, ViewGroup container,
Bundle savedinstancestate)
{
System.out.println ("Examplefragment--oncreateview");
Return Inflater.inflate (R.layout.example_fragment_layout, container, false);
}
@Override
public void OnPause ()
{
TODO auto-generated Method Stub
Super.onpause ();
System.out.println ("Examplefragment--onpause");
}
@Override
public void Onresume ()
{
TODO auto-generated Method Stub
Super.onresume ();
System.out.println ("Examplefragment--onresume");
}
@Override
public void OnStop ()
{
TODO auto-generated Method Stub
Super.onstop ();
System.out.println ("Examplefragment--onstop");
}
}
It's layout:
Copy Code code as follows:
Example_fragment_layout.xml
<?xml version= "1.0" encoding= "Utf-8"?>
<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= "left"
Android:textsize= "20dip"
android:text= "@string/fragment1"
/>
<textview
Android:layout_width= "Match_parent"
android:layout_height= "Wrap_content"
android:text= "@string/num1"
/>
<textview
Android:layout_width= "Match_parent"
android:layout_height= "Wrap_content"
android:text= "@string/num2"
/>
</LinearLayout>
a second fragment:
Copy Code code as follows:
Newfragment.java
Package com.example.learningfragment;
Import Android.os.Bundle;
Import android.support.v4.app.Fragment;
Import Android.view.LayoutInflater;
Import Android.view.View;
Import Android.view.ViewGroup;
public class Newfragment extends Fragment
{
Three methods that must be overloaded generally
@Override
public void OnCreate (Bundle savedinstancestate)
{
TODO auto-generated Method Stub
Super.oncreate (savedinstancestate);
System.out.println ("Newfragment--oncreate");
}
@Override
Public View Oncreateview (layoutinflater inflater, ViewGroup container,
Bundle savedinstancestate)
{
System.out.println ("Newfragment--oncreateview");
Return Inflater.inflate (R.layout.new_fragment_layout, container, false);
}
@Override
public void OnPause ()
{
TODO auto-generated Method Stub
Super.onpause ();
System.out.println ("Newfragment--onpause");
}
}
Copy Code code as follows:
New_fragment_layout.xml
<?xml version= "1.0" encoding= "Utf-8"?>
<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:textsize= "20dip"
Android:gravity= "left"
android:text= "@string/fragment2"
/>
<textview
Android:layout_width= "Match_parent"
android:layout_height= "Wrap_content"
android:text= "@string/num3"
/>
<textview
Android:layout_width= "Match_parent"
android:layout_height= "Wrap_content"
android:text= "@string/num4"
/>
</LinearLayout>
Main activity:
Copy Code code as follows:
Learnfragment.java
Package com.example.learningfragment;
Import Android.os.Bundle;
Import android.support.v4.app.FragmentActivity;
Import Android.support.v4.app.FragmentManager;
Import android.support.v4.app.FragmentTransaction;
Import Android.view.View;
Import Android.widget.Button;
public class Learnfragment extends fragmentactivity
{
Button btn1;
Button btn2;
Button Btn3;
Examplefragment Fragmente;
Newfragment Fragmentn;
Fragmentmanager Fragmentmanager;
@Override
public void OnCreate (Bundle savedinstancestate)
{
Super.oncreate (savedinstancestate);
Setcontentview (r.layout.activity_learn_fragment);
Findviews ();
Setlisteners ();
Get the object of the class that fragment admin needs
Fragmentmanager = Getsupportfragmentmanager ();
}
private void Findviews ()
{
BTN1 = (Button) Findviewbyid (R.ID.BTN1);
BTN2 = (Button) Findviewbyid (R.ID.BTN2);
Btn3 = (Button) Findviewbyid (R.ID.BTN3);
}
private void Setlisteners ()
{
First button, add a examplefragment
Btn1.setonclicklistener (New Button.onclicklistener ()
{
public void OnClick (View v)
{
Add Examplefragment to the program
Fragmente = new Examplefragment ();
Fragmenttransaction fragmenttransaction = Fragmentmanager.begintransaction ();
Fragmenttransaction.add (R.id.linear1,fragmente);
Fragmenttransaction.addtobackstack (NULL);
Fragmenttransaction.commit ();
}
}
);
The second button replaces the previously added fragment with a newfragment
Btn2.setonclicklistener (New Button.onclicklistener ()
{
public void OnClick (View v)
{
FRAGMENTN = new Newfragment ();
Fragmenttransaction fragmenttransaction = Fragmentmanager.begintransaction ();
Fragmenttransaction.replace (R.ID.LINEAR1,FRAGMENTN);
Fragmenttransaction.addtobackstack (NULL);
Fragmenttransaction.commit ();
}
}
);
Third button, remove fragment
Btn3.setonclicklistener (New Button.onclicklistener ()
{
public void OnClick (View v)
{
Fragmenttransaction fragmenttransaction = Fragmentmanager.begintransaction ();
Fragmenttransaction.remove (FRAGMENTN);
Fragmenttransaction.addtobackstack (NULL);
Fragmenttransaction.commit ();
}
}
);
}
}
Copy Code code as follows:
Activity_learn_fragment.xml
<linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"
Xmlns:tools= "Http://schemas.android.com/tools"
Android:id= "@+id/linear1"
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:textsize= "20dip"
Android:gravity= "Center_horizontal"
android:text= "@string/layout1"
/>
<button
Android:id= "@+id/btn1"
Android:layout_width= "Fill_parent"
android:layout_height= "Wrap_content"
android:text= "@string/btn1"
/>
<fragment
Android:name= "Com.example.learningfragment.ExampleFragment"
Android:id= "@+id/fragment1"
Android:layout_width= "Match_parent"
android:layout_height= "Wrap_content"
/>
<button
Android:id= "@+id/btn2"
Android:layout_width= "Fill_parent"
android:layout_height= "Wrap_content"
android:text= "@string/btn2"
/>
<linearlayout
Xmlns:android= "Http://schemas.android.com/apk/res/android"
Android:id= "@+id/linear2"
Android:layout_width= "Fill_parent"
android:layout_height= "Wrap_content"
android:orientation= "Vertical"
>
<textview
Android:layout_width= "Match_parent"
android:layout_height= "Wrap_content"
Android:textsize= "20dip"
Android:gravity= "Center_horizontal"
android:text= "@string/layout2"
/>
<button
Android:id= "@+id/btn3"
Android:layout_width= "Fill_parent"
android:layout_height= "Wrap_content"
android:text= "@string/btn3"
/>
</LinearLayout>
</LinearLayout>
Resources:
Copy Code code as follows:
Strings.xml
<resources>
<string name= "App_name" >LearningFragment</string>
<string name= "Hello_world" >hello world!</string>
<string name= "Menu_settings" >Settings</string>
<string name= "Title_activity_learn_fragment" >LearnFragment</string>
<string name= "LAYOUT1" >LinearLayout1</string>
<string name= "Layout2" >LinearLayout2</string>
<string name= "Fragment1" >FragmentType1</string>
<string name= "Fragment2" >FragmentType2</string>
<string name= "NUM1" >NO.1</string>
<string name= "num2" >NO.2</string>
<string name= "num3" >NO.3</string>
<string name= "Num4" >NO.4</string>
<string name= "BTN1" >add fragment</string>
<string name= "BTN2" >replace fragment</string>
<string name= "Btn3" >remove fragment</string>
</resources>
Program Run screenshot:
reference materials:
API guides:fragments
Http://developer.android.com/guide/components/fragments.html
Fragmentmanager Class Documentation:
Http://developer.android.com/reference/android/app/FragmentManager.html
Fragmenttransaction class Document
Http://developer.android.com/reference/android/app/FragmentTransaction.html