Go Translation [android basics] Communication between Android fragment

Source: Internet
Author: User
<span id="Label3"></p><p class="time"><p class="time">2014-2-14</p></p><p><p><span style="color: #ff0000; font-family: FangSong_GB2312; font-size: 14px;"><strong>This article is translated from the official Android training course, I am also a beginner, I think the official training is the best learning materials, so learning while translating, there is a bad place to translate, please correct ME.</strong></span></p></p><p><p></p></p><p><p>If we were to <strong>reuse</strong> the UI component of fragment in the development process, we should design fragment as a "self-contained", "modular" component that defines its own layout and Behavior. Once we have successfully defined such reusable fragment, we can associate them with the activity and then complete the UI assembly with the entire Application.</p></p><p><p>We often need a fragment to communicate with another fragment, such as clicking on a fragment and responding to another Fragment. <strong>all communication between the fragment and fragment is done by the activity between them and the 2 fragment cannot communicate directly with each other. </strong></p></p><p><p>The following is a detailed description of fragment direct communication steps.</p></p><p><p><strong>First step: define the interface</strong></p></p><p><p>In order to first implement fragment to communicate directly with the activity it binds to, we need to define an interface in the fragment class and implement it in the class of activity that it binds to. Fragment by capturing the implementation of the interface in the Onattach () lifecycle callback function, and then invoking the method in this interface to communicate with the Activity.</p></p><p><p>Here is an example:</p></p><p><p></p></p><pre name="code" class="java"><pre name="code" class="java">public class Headlinesfragment extends Listfragment { onheadlineselectedlistener mcallback; The container activity must implement this interface public interface Onheadlineselectedlistener {public void onarticleselected (int position); } @Override public void Onattach (activity activity) { super.onattach (activity); This ensures that the container activity has implemented this callback interface, and if not implemented, throws an exception try { mcallback = (onheadlineselectedlistener) activity; } Catch (classcastexception e) { throw new classcastexception (activity.tostring () + "must Implement Onheadlineselectedlistener "); } } ...}</pre></pre>Now the fragment can pass through the interface<code><code>OnHeadlineSelectedListener</code></code>Instances of<code><code>mCallback</code></code>Instance in the calling Interface.<code><code>onArticleSelected()</code></code>The method (or other methods defined in the Interface) sends messages to the Activity.<p><p></p></p><p><p>For example, when a user clicks on item in a list, the methods in the following fragment are Called. Fragment sends events to the upper level of activity through the callback Interface.</p></p><p><p></p></p><pre name="code" class="java"><pre name="code" class="java">@Override public void Onlistitemclick (ListView l, View v, int. position, Long Id) { //Send The event to the host a Ctivity mcallback.onarticleselected (position); }</pre></pre><strong><strong>Step two: Implement the interface</strong></strong>In order to receive callback events from fragment, the activity that contains this fragment must implement the interface defined in Fragment.<p><p></p></p><p><p>For example, The following code is an implementation of the above Interface.</p></p><p><p></p></p><pre name="code" class="java"><pre name="code" class="java">public static class Mainactivity extends Activity implements headlinesfragment.onheadlineselectedlistener{ ... public void onarticleselected (int position) { //the User selected the headline of a article from the Headlinesfragme Nt//do something-here-to- display that article }}</pre></pre><strong><strong>Step three: Send a message to fragment</strong></strong><p><p></p></p><p><p></p></p>The activity finds the fragment instance by calling the Findfragmentbyid () method, and then calls the public method of the instance Directly.<p><p>For example, Suppose the activity above might contain another fragment, which is used to display the data corresponding to the specific item in the previous Fragment. In this example, the activity transmits the data to the fragment, and the fragment follows the data to display the corresponding Content.</p></p><p><p></p></p><pre name="code" class="java">public static class Mainactivity extends Activity implements headlinesfragment.onheadlineselectedlistener{... public void onarticleselected (int position) {//the User selected the headline of a article from the HEADLINESF Ragment//do something-to-display that article articlefragment Articlefrag = (articlefragment) Getsupportfragmentmanager (). Findfragmentbyid (r.id.article_fragment); If (articlefrag! = Null) {//if Article Frag is available, we ' re in Two-pane layout ...//call a Me Thod in the articlefragment to update its content Articlefrag.updatearticleview (position); } else {//Otherwise, we ' re in the One-pane layout and must swap frags ...//Create fragment and GI ve it an argument for the selected article articlefragment newfragment = new Articlefragment (); Bundle args = new bundle (); Args.putint (articlefragment.arg_position, position); Newfragment.setarguments (args); Fragmenttransaction transaction = Getsupportfragmentmanager (). begintransaction (); Replace whatever is in the Fragment_container view with this Fragment,//and add the transaction to the BAC K stack so the user can navigate back transaction.replace (r.id.fragment_container, newfragment); Transaction.addtobackstack (null); Commit the transaction Transaction.commit (); } }}</pre><p><p> [to] [translate] communication between][android base]android fragment </p> </p></span>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.