Detailed explanation of the communication between Fragment and Activity in Android
Communication with activity
Although the implementation of fragments is activity-independent and can be used for multiple activities, each activity contains different instances of the same fragment.
Fragment can call the getActivity() method to easily get the object of the activity where it is located, and then you can find the controls in the activity (findViewById()).
For example:
ViewlistView =getActivity().findViewById();
Similarly, the activity can also find the fragments it contains through the FragmentManager method.
For example:
ExampleFragment fragment =(ExampleFragment)getFragmentManager().findFragmentById(.example_fragment
activity response fragment event
Sometimes, you may need fragment to share events with activity. A good idea is to define a callback interface in fragment and implement it in activity.
For example, it is the example of that news program, which has an activity that contains two fragments. fragmentA displays the news title, fragmentB displays the content corresponding to the title. fragmentA must tell activity when the user selects a certain title, and then activity tells fragmentB. fragmentB will display the corresponding content (why is it so troublesome? Just tell fragmentB directly? It's OK, but your fragment reduces the ability to reuse. Now I just need to tell my event to the host, and the host decides how to deal with it. Is this better reusability?). The following example is the OnArticleSelectedListener interface
Defined in fragmentA:
public static class FragmentA extends ListFragment{ ... //Container Activity must implement this interface public interface OnArticleSelectedListener{ public void onArticleSelected(Uri articleUri); } ...
Then activity implements the interface OnArticleSelectedListener, and notifies fragmentB in the method onArticleSelected(). When a fragment is added to the activity, the fragment's method onAttach() will be called. This method is suitable for checking whether the activity implements the OnArticleSelectedListener interface. The checking method is to type convert the instance of the incoming activity.
As shown below:
public static class FragmentA extends ListFragment{ OnArticleSelectedListener mListener; ... @Override public void onAttach(Activity activity){ (activity); try{ mListener =(OnArticleSelectedListener)activity; }catch(ClassCastException e){ throw new ClassCastException(()+"must implement OnArticleSelectedListener"); } } ...
If the activity does not implement that interface, the fragment throws a ClassCastException exception. If successful, the mListener member variable holds an instance of OnArticleSelectedListener. So fragmentA can call the mListener method to share events with activity. For example, if fragmentA is a ListFragment, each time an item of the list is selected, fragmentA's onListItemClick() method will be 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 =(ArticleColumns.CONTENT_URI,id); //Send the event and Uri to the host activity (noteUri); } ...
The parameter id passed in onListItemClick() is the selected row ID of the list. Another fragment uses this ID to obtain the title content from the program's ContentProvider.
If you have any questions, please leave a message or go to the community of this site to exchange and discuss. Thank you for reading. I hope it can help you. Thank you for your support for this site!