Hello everyone. fragment is now more and more widely used. I will replace fragment with fragment in the Chinese translation. At that time, Google's fragmentation was due to inconsistent screen sizes of Android devices. using a set of apps can solve the UI problems on tablets and mobile phones. it is very difficult for new users to make the fragments interact with the activity, because the fragments are attached to the activity. They are not a whole. Well, let's talk nonsense. Let's see how to make the fragments interact with the activity.
I designed it like this. There is a button in the activity. Click this button to display the following parts to a file. When this text is displayed, there will also be a button in the part, click the button in the part to change the part of the activity.
First, I first define an interface and a handler in the activity. The function of the interface is to allow the fragments to receive the activity instructions, while the handler is used to receive the instructions sent from the fragments. The activity is executing the operations as follows:
Package COM. example. fragmentactivity; import android. OS. bundle; import android. OS. handler; import android. OS. message; import android. app. activity; import android. support. v4.app. fragment; import android. support. v4.app. fragmentactivity; import android. support. v4.app. fragmenttransaction; import android. view. menu; import android. view. view; import android. view. view. onclicklistener; import android. widget. button; imp ORT android. widget. framelayout; public class mainactivity extends fragmentactivity implements onclicklistener {private button button1; private string fragmentname; private onbuttonclickedlistener buttonclickedlistener; /*** defines a handler command to receive yellow fragments from the activity */Public handler = new handler () {@ overridepublic void handlemessage (Message MSG) {super. handlemessage (MSG); If (MSG! = NULL) {Switch (MSG. what) {case 100:/*** receive the instruction from the yellow fragment, and the activity performs the replacement operation */fragmentname = fragment2.class. getname (); replacefragment (R. id. framelayout1, fragmentname); break; default: Break ;}}}; @ overrideprotected void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. activity_main); button1 = (button) findviewbyid (R. id. button1); button1.setonclicklistener (this );/*** Load yellow fragments */fragmentname = fragment1.class. getname (); replacefragment (R. id. framelayout1, fragmentname);} @ overridepublic void onclick (view v) {Switch (v. GETID () {case R. id. button1: If (buttonclickedlistener! = NULL) {buttonclickedlistener. onclicked ("activity: I don't want this yellow fragment anymore. click the button below to change it to a green fragment") ;}break; default: Break ;}} /*** load fragment */protected void replacefragment (INT viewresource, string fragmentname) {fragmenttransaction Ft = getsupportfragmentmanager (). begintransaction (); fragment = fragment. instantiate (this, fragmentname); ft. replace (viewresource, fragment);ft.settransition(fragmenttransaction.transit_fragment_open);ft.commit();getsupportfragmentmanager().exe cutependingtransactions ();} /*** define an interface * @ author zqy **/public interface onbuttonclickedlistener {/*** to transfer a value * @ Param S */Public void onclicked (string S );} /***** @ Param buttonclickedlistener * write a public Method */Public void setbuttonclickedlistener (onbuttonclickedlistener buttonclickedlistener) {This. buttonclickedlistener = buttonclickedlistener ;}}
This is the code in the activity. We are writing two fragments as follows:
Fragment1:
Package COM. example. fragmentactivity; import android. OS. bundle; import android. OS. handler; import android. OS. message; import android. support. v4.app. fragment; import android. view. layoutinflater; import android. view. view; import android. view. view. onclicklistener; import android. view. viewgroup; import android. widget. button; import android. widget. textview; import android. widget. toast; import COM. example. fragmentactivity. mainactivity. onbuttonclickedlistener; public class fragment1 extends fragment implements onclicklistener {private textview; private button; private handler; @ overridepublic void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); mainactivity activity = (mainactivity) getactivity ();/*** get activity handler */handler = activity. handler;/*** Add a listener */activity. setbuttonclickedlistener (New onbuttonclickedlistener () {@ overridepublic void onclicked (string s) {/** display content from activity */textview. settext (s); button. setvisibility (view. visible) ;}}) ;}@ overridepublic view oncreateview (layoutinflater Inflater, viewgroup container, bundle savedinstancestate) {return Inflater. inflate (R. layout. item1, null) ;}@ overridepublic void onactivitycreated (bundle savedinstancestate) {super. onactivitycreated (savedinstancestate); textview = (textview) getactivity (). findviewbyid (R. id. textview1); button = (button) getactivity (). findviewbyid (R. id. button2); button. setonclicklistener (this) ;}@ overridepublic void onclick (view v) {Switch (v. GETID () {case R. id. button2:/*** send command to activity */handler. sendemptymessage (100); break; default: break ;}}
This fragment allows its activity to implement this interface. When the value in the interface is displayed on the page, click the button in the fragment to send a message to its activity. here I sent 100.
Fragment2:
package com.example.fragmentactivity;import android.os.Bundle;import android.support.v4.app.Fragment;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;public class Fragment2 extends Fragment {@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {// TODO Auto-generated method stubreturn inflater.inflate(R.layout.item2, null);}}
This is the end of writing. in the future, the project will not be afraid of communication between fragments and the activity. The UI will be ugly, haha. If not, you can leave a message below. I will upload the demo to your resources, for download. Thank you!
Click here to download the source code
Communication between fragment and Activity