In some cases, fragment and activity sharing events may be required. Defining a callback interface within fragment is a good method and requires that the callback method be implemented by the activity holding it. When an activity receives a callback through an interface, it can share information with other fragment in the layout if necessary.
For example, if a news application has two fragment items in an activity, one for displaying the article list (fragment a) and the other for displaying the article content (fragment B) --- when the list item is selected, fragment A must tell activity so that it can tell fragment B to display the corresponding article. In the following example, the onarticleselectedlistener interface is declared in fragment.
Public static class fragmenta extends listfragment {
...
// Container activity must implement this interface
Public interface onarticleselectedlistener {
Public void onarticleselected (URI articleuri );
}
...
}
Then, the activity holding this fragment must implement the onarticleselectedlistener interface, and rewrite the onarticleselected () method to notify fragment B of the event from fragment. Make sure that the activity holding fragment implements this interface. The onattach () callback method of fragment A (the system calls this method when fragment is added to the activity) is converted to onattach () by the type conversion method () the input activity is used to instantiate an onarticleselectedlistener instance.
Public static class fragmenta extends listfragment {
Onarticleselectedlistener mlistener;
...
@ Override
Public void onattach (activity ){
Super. onattach (activity );
Try {
Mlistener = (onarticleselectedlistener) activity;
} Catch (classcastexception e ){
Throw new classcastexception (activity. tostring () + "must implement onarticleselectedlistener ");
}
}
...
}
If this activity does not implement this interface, fragment will throw a classcastexception. If successful, the mlistener member will have a reference to the onarticleselectedlistener object implemented by the activity, so that fragment A can define the callback method and activity sharing event through the onarticleselectedlistener interface. For example, if listfragment inherits fragment A, the system calls the onlistitemclick () method in fragment each time you click a list item, and then calls the onarticleselected () method and activity sharing event:
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 );
}
...
}
The ID parameter passed to onlistitemclick () is the row ID of the project to be clicked. Activity (or other fragment) uses this ID to obtain the corresponding article from the contentprovider object of the application.
For more information about using the valid content providers, see the content providers documentation.