Fragment was introduced after the Android3.0. Can be used to do the bottom menu, now many apps are useful to this bottom menu. Of course tabhost can also be used to do the bottom menu, but fragment to do, animation effects these can be done more dazzling.
Fragment and activity are capable of transmitting these parameters to each other. But fragment can not directly transfer data to another fragment, but with activity, first to the activity, how to pass activity to fragment. Therefore, it is critical to implement data transfer between fragment and activity.
(1) First create a interface interface Ibncalllistener for data transfer between the two.
Package Com.util; /** */Publicinterface ibtncalllistener {publicvoid transfermsg ();}
(2) Create the activity class to implement the Ibtncalllistener interface, overload the Onattachfragment (Fragment Fragment) method and the Transfermsg () method, and then Transfermsg () Method is called according to your project needs.
Packagecom.activity;/*** * Main interface class *fragment is something after 3.0, in order to use fragment in the lower version will use the Android-support-v4.jar compatibility package, * and fragmentactivity is in this compatibility package, It provides some ways to manipulate fragment, which functions the same as 3.0 and later versions of activity. */ Public classMainactivityextendsFragmentactivityImplementsibtncalllistener{//RadioButton PrivateRadioButton radiobtn; PrivateIntent Intent; Privatebundle Bundle; PrivateIbtncalllistener Mbtncalllistener; PrivateBundle Sendbundle; Privatemyfragment mfragment; Fragmenttransaction ft; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); //Set Untitledrequestwindowfeature (Window.feature_no_title); Setcontentview (R.layout.main); } ... @Override Public voidonattachfragment (Fragment Fragment) {Try{Mbtncalllistener=(Ibtncalllistener) fragment; } Catch(Exception e) {}Super. Onattachfragment (fragment); } @Override Public voidtransfermsg () {radiobtn.setcompounddrawableswithintrinsicbounds (0, R.drawable.tab_found_pressed_icon, 0, 0); Radiobtn.settextcolor (Mfound.getresources (). GetColor (R.COLOR.TOPBAR_BG)); if(Mfoundfragment = =NULL){ //key Code, sure to have these three lines of code, others according to their own project needsMfragment =Newmyfragment (); Sendbundle=NewBundle (); Sendbundle.putstring ("Test", "Android"); //transfermsg ();mfragment.setarguments (Sendbundle); Ft.add (r.id.fg_content,mfragment); } ft.show (Mfoundfragment); Ft.commit (); }}
(3) Fragment class implements Ibtncalllistener interface class
/*** Myfragment class*/ Public classMyfragmentextendsFragmentImplementsibtncalllistener{Privatebundle Bundle; Ibtncalllistener Mbtnlistener; PrivateString test; //@Nullable@Override PublicView Oncreateview (layoutinflater inflater, ViewGroup container, Bundle savedinstancestate) {View= Inflater.inflate (R.layout.found,container,false); Transfermsg (); returnview; }} @Override Public voidonactivitycreated (Bundle savedinstancestate) {Super. onactivitycreated (savedinstancestate); } @Override Public voidOnattach (activity activity) {Try{Mbtnlistener=(Ibtncalllistener) activity; } Catch(Exception e) {//Todo:handle Exception Throw NewClassCastException (activity.tostring () + "must implement Mbtnlistener"); } Super. Onattach (activity); } @Override Public voidtransfermsg () {bundle=getarguments (); Test= bundle.getstring ("Test"); System.out.println ("Information transmitted by the activity."); Toast.maketext (getactivity (), Test,1000). Show (); }}
Data communication between Android fragment and activity