1. public void Onattach (activity activity)
This method will be called back when fragment is added to the activity, and this method will only be recalled once
2. public void OnCreate (Bundle saveinstancestate)
A callback is called when the fragment is created, only once
3. public void onactivitycreated (Bundle saveinstancestate)
Called when the activity on which the fragment is started is completed
4. Public View Oncreateview (layoutinflater inflater,viewgroup container,bundle saveinstancestate)
Callback This method every time you create a view component that will draw fragment
5. public void OnStart ()
Start fragment
6. public void Onresume ()
The callback is called when the fragment is restored, and the Onresume () method will be called after the call to the OnStart () method
7. public void OnPause ()
Pause Fragment
8. public void OnStop ()
Stop fragment
9. public void Ondestoryview ()
Called when destroying the view component contained by fragment
public void Ondestory ()
Called when destroying fragment
One. public void Ondetch ()
Fragment is called when it is deleted in activity, and this method is only called once
--------------------------------------------------------------------
Fragment communication with the activity
1. Fragment can call the Getactivity () method to get the activity it is in.
2. Activity can call Fragmentmanager's Findfragmentbyid () or Findfragmentbytag () method to get fragment
3. Activity-----àfragment: Create bundle packets in activity and invoke fragment's setarguments (Bundle bundle) method
4. Fragment---àactivity: You need to define an internal callback interface in Fragment, and then let the activity that contains the Fragment implement the interface callback. So fragment can call the callback method to pass the data to the activity
--------------------------------------------------------------------Eg:
&& through fragment and activity communication to achieve the above operation
1. Analysis layout, requires two layout
A layout of a linearlayout[s1] contains <EditText> <Button>
A <fragment> included for layout for a linerlayout
2. Write Myfragment.java after layout is complete and inherit fragment
A method that contains a Setcontentview ()
Public View Oncreateview (layoutinflater inflater, ViewGroup container,
Bundle savedinstancestate) {
View view = Inflater.inflate (R.layout.fragment2, container, false);
TextView TV = (TextView) View.findviewbyid (R.id.text);
String text=getarguments (). Get ("name") + "" "[S2];
Tv.settext (text);
return view;
3. Write Myactivity.java, and inherit activity
① control initialization and other operations omitted
② to increase the Click event of a button
Send.setonclicklistener (New Onclicklistener () {
public void OnClick (View v) {
String text = Editext.gettext (). toString ();
MYFRAGMENT5 fragment5 = new MyFragment5 (); [S3]
Bundle bundle = new bundle (); [S4]
Bundle.putstring ("name", text); [S5]
Fragment5.setarguments (bundle); [S6]
Fragmentmanager Fragmentmanager = Getfragmentmanager ();
Fragmenttransaction BeginTransaction = Fragmentmanager
. BeginTransaction (); [S7]
Begintransaction.add (R.id.layout, FRAGMENT5, "fragment5"); [S8]
Begintransaction.commit ();
[S1] contains ID
[S2] for accepting values
[S3] Creating an Fragment object
[S4] Declaring packets
Data assignment in [S5] Packets
[S6] passing data to fragment
[S7] Creating a transaction
[S8] Adding a View object "layout <<<ß------Data" to the current view
Android growth Diary-fragment life cycle and activity communication