Android-fragment (iii) communication between different Fragment

Source: Internet
Author: User

In fragment's Java file, you can use Getactivity () to get the activity that invokes it.

And then find another fragment, to communicate

getactivity (). Getfragmentmanager (). Findfragmentbyid (r.id.fragment_list);

However, this coupling is too high to facilitate subsequent modification operations

The communication between the fragment and its attached activity should be done by the activity.

cannot be a direct communication between multiple fragment

The best way to communicate between fragment and its attached activity:

1. Define an interface in the fragment that initiates the event, and declare your method in the interface

2. Require activity to implement this interface in the Onattach method

3. Implement this method in activity

For example, 2 fragment are arranged in an activity, and communication between them depends on activity to complete

Code: Liststoreactivity.java newitemfragment.java Liststorefragment.java

The layout file is: liststore.xml new_item_fragment.xml

Prepare the layout file:

Liststore.xml placed 2 fragment in LinearLayout, pointing to 2 fragment files respectively

    <FragmentAndroid:id= "@+id/fragment_new"Android:name= "Com.rust.liststore.NewItemFragment"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content" />    <FragmentAndroid:id= "@+id/fragment_listview"Android:name= "Com.rust.liststore.ListStoreFragment"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content" />

Place a edittext and a button in the new_item_fragment.xml side

    <EditTextAndroid:id= "@+id/edittx_new"Android:layout_width= "0DP"Android:layout_weight= "4"Android:layout_height= "Wrap_content"Android:hint= "@string/hint_new" />    <ButtonAndroid:id= "@+id/btn_add_new_item"Android:layout_width= "0DP"Android:layout_weight= "1"Android:layout_height= "Wrap_content"Android:text= "@string/hint_add_new_item"        />

Liststorefragment.java

 Public class extends listfragment{///inherit from Listfragment, already packaged listview///do not need to write your own listview }

Newitemfragment.java Main code is as follows

   /*** Declare an interface that defines the method that is passed to the activity * bound activity must implement this method*/     Public InterfaceOnnewitemaddedlistener { Public voidnewitemadded (String content); }    PrivateOnnewitemaddedlistener Onnewitemaddedlistener; PrivateButton Btnadditem; /*Replication Onattach Method*/@Override Public voidOnattach (activity activity) {Super. Onattach (activity); Try{Onnewitemaddedlistener=(Onnewitemaddedlistener) activity; } Catch(classcastexception e) {Throw NewClassCastException (activity.tostring () + "must implement Onnewitemaddedlistener"); }    }        

Liststoreactivity.java

Load main view Setcontentview (R.layout.liststore);

Two fragment to communicate via Liststoreactivity.java

Obtain an instance of Liststorefragment in the OnCreate method, and the Newitemadded method, adding business logic to it

 Public classListstoreactivityextendsActivityImplementsonnewitemaddedlistener{PrivateArraylist<string>data; PrivateArrayadapter<string>adapter; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.liststore); Data=NewArraylist<string>(); //load the data into the adapteradapter =NewArrayadapter<string> ( This, Android.        R.layout.simple_list_item_1, data); //Listfragment does not need to define a listviewListstorefragment liststorefragment =(liststorefragment) Getfragmentmanager (). Findfragmentbyid (R.id.fragment_listview);    Liststorefragment.setlistadapter (adapter); } @Override Public voidnewitemadded (String content) {//The method in the replication interface, where the business code is implemented        if(!content.equals ("") {data.add (content);        Adapter.notifydatasetchanged (); }    }}

At this point, the communication is completed

Android-fragment (iii) communication between different Fragment

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.