• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1page.title=Communicating with Other Fragments
2parent.title=Building a Dynamic UI with Fragments
3parent.link=index.html
4
5trainingnavtop=true
6previous.title=Building a Flexible UI
7previous.link=fragment-ui.html
8
9@jd:body
10
11<div id="tb-wrapper">
12  <div id="tb">
13    <h2>This lesson teaches you to</h2>
14<ol>
15  <li><a href="#DefineInterface">Define an Interface</a></li>
16  <li><a href="#Implement">Implement the Interface</a></li>
17  <li><a href="#Deliver">Deliver a Message to a Fragment</a></li>
18</ol>
19
20    <h2>You should also read</h2>
21    <ul>
22      <li><a href="{@docRoot}guide/components/fragments.html">Fragments</a></li>
23    </ul>
24
25<h2>Try it out</h2>
26
27<div class="download-box">
28 <a href="http://developer.android.com/shareables/training/FragmentBasics.zip"
29class="button">Download the sample</a>
30 <p class="filename">FragmentBasics.zip</p>
31</div>
32
33  </div>
34</div>
35
36<p>In order to reuse the Fragment UI components, you should build each as a completely
37self-contained, modular component that defines its own layout and behavior.  Once you
38have defined these reusable Fragments, you can associate them with an Activity and
39connect them with the application logic to realize the overall composite UI.</p>
40
41<p>Often you will want one Fragment to communicate with another, for example to change
42the content based on a user event.  All Fragment-to-Fragment communication is done
43through the associated Activity.  Two Fragments should never communicate directly.</p>
44
45
46<h2 id="DefineInterface">Define an Interface</h2>
47
48<p>To allow a Fragment to communicate up to its Activity, you can define an interface
49in the Fragment class and implement it within the Activity.  The Fragment captures
50the interface implementation during its onAttach() lifecycle method and can then call
51the Interface methods in order to communicate with the Activity.</p>
52
53<p>Here is an example of Fragment to Activity communication:</p>
54
55<pre>
56public class HeadlinesFragment extends ListFragment {
57    OnHeadlineSelectedListener mCallback;
58
59    // Container Activity must implement this interface
60    public interface OnHeadlineSelectedListener {
61        public void onArticleSelected(int position);
62    }
63
64    &#64;Override
65    public void onAttach(Activity activity) {
66        super.onAttach(activity);
67
68        // This makes sure that the container activity has implemented
69        // the callback interface. If not, it throws an exception
70        try {
71            mCallback = (OnHeadlineSelectedListener) activity;
72        } catch (ClassCastException e) {
73            throw new ClassCastException(activity.toString()
74                    + " must implement OnHeadlineSelectedListener");
75        }
76    }
77
78    ...
79}
80</pre>
81
82<p>Now the fragment can deliver messages to the activity by calling the {@code
83onArticleSelected()} method (or other methods in the interface) using the {@code mCallback}
84instance of the {@code OnHeadlineSelectedListener} interface.</p>
85
86<p>For example, the following method in the fragment is called when the user clicks on a list
87item. The fragment uses the callback interface to deliver the event to the parent activity.</p>
88
89<pre>
90    &#64;Override
91    public void onListItemClick(ListView l, View v, int position, long id) {
92        // Send the event to the host activity
93        mCallback.onArticleSelected(position);
94    }
95</pre>
96
97
98
99<h2 id="Implement">Implement the Interface</h2>
100
101<p>In order to receive event callbacks from the fragment, the activity that hosts it must
102implement the interface defined in the fragment class.</p>
103
104<p>For example, the following activity implements the interface from the above example.</p>
105
106<pre>
107public static class MainActivity extends Activity
108        implements HeadlinesFragment.OnHeadlineSelectedListener{
109    ...
110
111    public void onArticleSelected(int position) {
112        // The user selected the headline of an article from the HeadlinesFragment
113        // Do something here to display that article
114    }
115}
116</pre>
117
118
119
120<h2 id="Deliver">Deliver a Message to a Fragment</h2>
121
122<p>The host activity can deliver messages to a fragment by capturing the {@link
123android.support.v4.app.Fragment} instance
124with {@link android.support.v4.app.FragmentManager#findFragmentById findFragmentById()}, then
125directly call the fragment's public methods.</p>
126
127<p>For instance, imagine that the activity shown above may contain another fragment that's used to
128display the item specified by the data returned in the above callback method. In this case,
129the activity can pass the information received in the callback method to the other fragment that
130will display the item:</p>
131
132<pre>
133public static class MainActivity extends Activity
134        implements HeadlinesFragment.OnHeadlineSelectedListener{
135    ...
136
137    public void onArticleSelected(int position) {
138        // The user selected the headline of an article from the HeadlinesFragment
139        // Do something here to display that article
140
141        ArticleFragment articleFrag = (ArticleFragment)
142                getSupportFragmentManager().findFragmentById(R.id.article_fragment);
143
144        if (articleFrag != null) {
145            // If article frag is available, we're in two-pane layout...
146
147            // Call a method in the ArticleFragment to update its content
148            articleFrag.updateArticleView(position);
149        } else {
150            // Otherwise, we're in the one-pane layout and must swap frags...
151
152            // Create fragment and give it an argument for the selected article
153            ArticleFragment newFragment = new ArticleFragment();
154            Bundle args = new Bundle();
155            args.putInt(ArticleFragment.ARG_POSITION, position);
156            newFragment.setArguments(args);
157
158            FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
159
160            // Replace whatever is in the fragment_container view with this fragment,
161            // and add the transaction to the back stack so the user can navigate back
162            transaction.replace(R.id.fragment_container, newFragment);
163            transaction.addToBackStack(null);
164
165            // Commit the transaction
166            transaction.commit();
167        }
168    }
169}
170</pre>
171
172
173
174
175
176
177
178
179
180