Life cycle and communication of [Android] fragment

Rewrite the lifecycle method

All fragments are attached to an activity. Fragments can be created only when the activity is displayed

Onattach, when attached to an activity

Oncreate, when the interface is created

Oncreateview. Return the view object after creation. It is important

Onactivitycreate, when the activity is created

OnStart, when the interface becomes visible to the user

Onresume, when the focus is obtained

Onpause, when you lose focus

Onstop, when the interface is not visible

Ondestroy, when the interface is destroyed

Ondestroyview, view object destruction, important

Ondestroy, fragment destruction

Ondetach, unmount

Communication between fragments

There are two fragments in the layout interface. Modify the contents of another fragment in one fragment

One fragment 1 has a button and the other fragment 2 has a textview. Click the button to modify the text

Override the oncreateview() method of fragment1,

Call the inflate () method of the layouteinflator object to convert the layout file into a view object

Call the findviewid() method of the view object to get the button object

Call the setonclicklistener() method of the button object, set the click event, and implement the onclick() method internally

In the onclick () method

Call getactivity () method to get the container activity object, and call getfragmentmanager () method to get the fragmentmanager object,

Call findfragmentbyid() of the fragmentmanager object to obtain the fragment2 object. Parameter: the control ID of another fragment2

In the fragment2 class, you need to expose a method to modify your internal control content

Call the exposed method in the fragment object obtained in the previous step

Use the activity container in the middle to achieve the purpose of communication between two fragments

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>