Communication with other Fragment the next lesson, this lesson teaches you
- Defining interfaces
- Implementing interfaces
- Send a message to a Fragment
You also need to read
Try the Download sample
Fragmentbasics.zip
In order to reuse the Fragment UI components, you should construct each Fragment into a fully self-contained modular component that defines its own layout and behavior. Once you have defined these reusable Fragment, you can associate them to an Activity and use the application logic to connect them to implement the overall combined UI.
Typically you want a Fragment to communicate with other Fragment, such as changing content based on user events. All fragment-to-fragment communication is done through the associated Activity. Two Fragment should never communicate directly.
Defining interfaces
To allow a Fragment to communicate with its activity, you can define an interface in the Fragment class and implement it in the activity. Fragment captures the implementation of an interface in its Onattach () life-cycle approach, and can then invoke the interface's methods to communicate with the Activity.
Here is an example of a Fragment to Activity communication:
Public class headlinesfragment extends listfragment { Onheadlineselectedlistener Mcallback; //Container Activity must implement this interface Public Interface Onheadlineselectedlistener { Public void onarticleselected(int position); } @Override Public void Onattach(Activity Activity) { Super.Onattach(Activity); //Ensure that the container activity implements the callback interface. If not, throw an exception Try { Mcallback = (Onheadlineselectedlistener) Activity; } Catch (classcastexception e) { Throw New classcastexception(Activity.toString() + "must implement Onheadlineselectedlistener"); } } ...}
Now, this fragment can send messages to the activity by invoking the method of the OnHeadlineSelectedListener mCallback instance of the interface onArticleSelected() (or other methods in the interface).
For example, when a user taps a list item, the following method in fragment is called. This fragment uses the callback interface to send events to the parent activity.
@Override Public void Onlistitemclick(ListView L, View v, int position, Long ID) { //Send event to host activity Mcallback.onarticleselected(position); }Implementing interfaces
In order to receive fragment event callbacks, the host activity must implement the interfaces defined in this fragment class.
For example, the following activity implements the interface in the example above.
Public Static class mainactivity extends Activity Implements headlinesfragment.Onheadlineselectedlistener{ ... Public void onarticleselected(int position) { //user selected the article title in Headlinesfragment //Do something here show that article }}Send a message to a Fragment
The host activity uses the findFragmentById() captured Fragment instance and then calls the fragment public method directly to send the message.
For example, imagine that the previous activity might contain another fragment used to display the item specified by the data returned in the callback method above. In this case, the activity can pass the information received in the callback method to other fragment that display the item:
Public Static class mainactivity extends Activity Implements headlinesfragment.Onheadlineselectedlistener{ ... Public void onarticleselected(int position) { //user selected the article title in Headlinesfragment //Do something here show that article articlefragment Articlefrag = (articlefragment) Getsupportfragmentmanager().Findfragmentbyid(R.ID.article_fragment); if (Articlefrag != NULL) { //If article fragment is available, we are using two columns of layout ... //Call the Articlefragment method to update its contents Articlefrag.Updatearticleview(position); } Else { //Otherwise, we are using a single column layout and must switch fragment ... //Create fragment and give it a selection of arguments for the article articlefragment newfragment = New articlefragment(); Bundle args = New Bundle(); args.Putint(articlefragment.arg_position, position); newfragment.setarguments(args); fragmenttransaction Transaction = Getsupportfragmentmanager().BeginTransaction(); //Use this fragment to replace something in the Fragment_container view //and add the transaction to the return stack so that the user can navigate back Transaction.Replace(R.ID.Fragment_container, newfragment); Transaction.Addtobackstack(NULL); //Commit a transaction Transaction.Commit(); } }}Last lesson Next lesson
Android Training-start-use Fragment to construct dynamic ui-communication with other Fragment