Communication with activity
Although the fragment implementation is independent of activity, it can be used for multiple activity, but each activity contains a different instance of the same fragment.
Fragment can call the Getactivity () method to get the object of its activity easily, and then it can find the controls in the activity (Findviewbyid ()). For example:
Viewlistview =getactivity (). Findviewbyid (r.id.list); Similarly, the activity can be fragmentmanager to find the frament that it contains. For example:
- Examplefragment fragment = (examplefragment) Getfragmentmanager (). Findfragmentbyid (r.id.example_fragment
Activity responds to fragment events
Sometimes, you may need to share events with the activity fragment. A good idea is to define a callback interface in fragment and then implement it in the activity.
For example, it is also an example of the news program, which has a activity,activity containing two fragment. Fragmenta Displays news headlines, FRAGMENTB displays the contents of the title. Fragmenta must tell the activity when the user chooses a title, and then the activity tells FRAGMENTB,FRAGMENTB to show the corresponding content (why is it so troublesome?). Direct Fragmenta tell Fragmentb not on the line? Yes, but your fragment reduces the ability to reuse. Now all I have to do is tell the host about my event, and it's up to the host to decide what to do, so is reusability better? )。 In the following example, the Onarticleselectedlistener interface is defined in Fragmenta:
- Public static class Fragmenta extends listfragment{
- ...
- //container Activity must implement this interface
- public interface onarticleselectedlistener{
- public void onarticleselected (Uri articleuri);
- }
- ...
The activity then implements the interface Onarticleselectedlistener, notifying Fragmentb in Method Onarticleselected (). When fragment is added to the activity, the Fragment Method Onattach () is called, which is suitable for checking that the activity implements the Onarticleselectedlistener interface. The check method is to type-Convert the instance of the incoming activity as follows:
- Public static class Fragmenta extends listfragment{
- Onarticleselectedlistener Mlistener;
- ...
- @Override
- public void Onattach (activity activity) {
- Super.onattach (activity);
- try{
- Mlistener = (onarticleselectedlistener) activity;
- }catch (ClassCastException e) {
- throw New ClassCastException (activity.tostring () +"must implement Onarticleselectedlistener");
- }
- }
- ...
If the activity does not implement that interface, fragment throws a ClassCastException exception. If successful, the Mlistener member variable holds the instance of Onarticleselectedlistener. Fragmenta can then invoke the Mlistener method to share the event with the activity. For example, if Fragmenta is a listfragment, each time you select an item in the list, the Onlistitemclick () method of Fragmenta is called, and onarticleselected () is called in this method. To share events with the activity, as follows:
- Public static class Fragmenta extends listfragment{
- Onarticleselectedlistener Mlistener;
- ...
- @Override
- public void Onlistitemclick (ListView l,view v,int position,long id) {
- //append The clicked item ' s row ID with the content provider Uri
- Uri Noteuri =contenturis.withappendedid (articlecolumns.content_uri,id);
- //send the event and Uri to the host activity
- Mlistener.onarticleselected (Noteuri);
- }
- ...
Onlistitemclick () The parameter ID passed in is the selected row ID of the list, and the other fragment uses this ID to get the contents of the title from the ContentProvider of the program.
Copy to Google TranslateTranslation Results
Android fragment Detailed (V): Fragment and Activity communication