The life cycle of the fragment, the specific method when the call will not say.
Two common ways of using fragment and communication in activity are
1. The Static Way
(1) Define a class that inherits fragment, such as Myfragment, where the Replication Oncreateview () method and the Onattach () method
(2) Oncreateview () loads some layout objects and sets the view object and returns a View object, which is used when Onattach () communicates.
(3) Refer to the fragment inheriting class in the layout file of the activity layouts (note setting the Name property to Myfragment's all-inclusive name, and setting the ID or tag one of the data)
The above can be used in the activity fragment, if you want to communicate with each other, and then below (including the above 1 2 3 steps)
(4.1) activity sends data to fragment
(4.2) Define private member variables in Myfragment, and implement get and set methods to receive data, directly on the code
Fragmentmanager Fragmentmanager = Getfragmentmanager ();
Fragment Findfragmentbyid = Fragmentmanager.findfragmentbyid (r.id.fragment);
Myfragment myfragment= (myfragment) findfragmentbyid;//Find fragment
Myfragment.setaa ("avtivity value");
(4.3) data can be obtained at myfragment via Getaa ()
(5.1) Fragmenty send data to Activit
(5.2) through member variables
(5.4) activity implements the MyInterface interface, it is possible to get the values passed in through the method parameters in the interface
2 Dynamic Way
(1) Define a class that inherits fragment, such as Myfragment, where the Replication Oncreateview () method and the Onattach () method
(2) Dynamic loading of fragment in activity
Myfragment Fragment2 = new Myfragment ();
Fragmentmanager Fragmentmanager = Getfragmentmanager ();
Fragmenttransaction BeginTransaction = Fragmentmanager.begintransaction ();
Begintransaction.add (R.id.ll_load_fragment, Fragment2);//Add Fragment2 to a layout with an ID of ll_load_fragment
Begintransaction.commit ();
(3.1) Activity to fragment value
Myfragment fragment = new Myfragment ();
Fragmentmanager Fragmentmanager = Getfragmentmanager ();
Fragmenttransaction BeginTransaction = Fragmentmanager.begintransaction ();
Bundle bundle = new bundle ();
String value = "Activity-transmitted value";
Bundle.putstring ("name", value);
Fragment.setarguments (bundle);//Add bundle data to Fragment
Begintransaction.add (r.id.ll_load_fragment, Fragment, "Fragment");
Begintransaction.commit ();
(3.2) Get the value by Getarguments (). Get (key) in Myfragment
(4.1) Fragment transfer value to activity
Defining an interface in Myfragment
Private MyInterface MyInterface;
public interface myinterface{
public void Sendmsg (Object object);
}
(4.2) Convert activity to MyInterface through Onattach (activity activity) method
public void Onattach (activity activity) {
Myinterface= (myinterface) activity;
Super.onattach (activity);
}
Use the interface to realize fragment and activity information transmission.
Summarize the way the data is exchanged between fragment and activity:
Dynamic
* 1 activity transmits data to fragment
Fragment.setarguments (bundle)
* 2 Fragment data transfer to activity
Defines a callback interface within the fragment. Let the fragment containing the
The activity implements the interface. This callback method can be called by fragment
Passing data to activity
Static
* 1 activity transmits data to fragment
Call the set method of the fragment inheriting class directly
* 2 Fragment data transfer to activity
Call the Get method of the fragment inheriting class directly
is to pass the value of each other by fragment the Get and set methods of inheriting class member variables.
Flagment using. zip
Use of fragment in Android and communication with activity