The use of fragment can make our application more flexible to adapt to various types of Android devices, but for fragment and activity between the communication, a lot of friends should be more unfamiliar, below we have an example to see how to achieve.
First, Activity->fragment pass data1.main.xml:
<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http// Schemas.android.com/tools " android:layout_width=" match_parent " android:id=" @+id/rl_fragment " android:orientation= "vertical" android:layout_height= "match_parent" > <edittext android:id= "@ +id/et_input " android:layout_width=" match_parent " android:layout_height=" wrap_content "/> < Button android:id= "@+id/btn_send" android:layout_width= "wrap_content" android:layout_height= " Wrap_content " android:text=" send "/></linearlayout>
2.fragment.xml:
<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android " android:layout_width=" match_parent " android:layout_height=" match_parent " android:o rientation= "vertical" > <textview android:id= "@+id/tv_fragment" android:layout_height= "Wrap_ Content " android:layout_width=" wrap_content " /></linearlayout>
3.mainactivity.java:
Package Com.example.fragementcommunication;import Android.app.activity;import Android.app.fragmentmanager;import Android.app.fragmenttransaction;import Android.os.bundle;import Android.view.view;import Android.view.view.onclicklistener;import Android.view.window;import Android.widget.button;import Android.widget.edittext;import Android.widget.toast;public class Mainactivity extends Activity {private EditText Mmainactivityet;private Button Msendbutton; @Overrideprotected void OnCreate (Bundle savedinstancestate) { Super.oncreate (savedinstancestate); Requestwindowfeature (Window.feature_no_title); SetContentView ( R.layout.activity_main); Mmainactivityet = (EditText) Findviewbyid (r.id.et_input); Msendbutton = (Button) Findviewbyid (r.id.btn_send); Msendbutton.setonclicklistener (new Onclicklistener () {@Overridepublic void OnClick (View v) {String Text = Mmainactivityet.gettext (). toString (); Myfragment myfragment = new Myfragment (); Bundle bundle = new bundle (); Bundle.putstring ("Input", text); myfragment.Setarguments (bundle);//pass Stringfragmentmanager Manager = Getfragmentmanager (); Fragmenttransaction transaction = Manager.begintransaction (); Transaction.add (R.id.rl_fragment, Myfragment, " Myfragment "); Transaction.commit (); Toast.maketext (Mainactivity.this, "Send data to Fragment" + text, Toast.length_short). Show ();}});}}
4.myfragment.java:
Package Com.example.fragementcommunication;import Android.app.fragment;import Android.os.bundle;import Android.view.layoutinflater;import Android.view.view;import Android.view.viewgroup;import Android.widget.TextView Import Android.widget.toast;public class Myfragment extends Fragment {@Overridepublic View oncreateview ( Layoutinflater inflater, ViewGroup container, Bundle savedinstancestate) {View view = Inflater.inflate (R.layout.fragmet , null); TextView Mfragmenttextview = (TextView) View.findviewbyid (r.id.tv_fragment); String string = Getarguments (). getString ("input");//Get Data; Mfragmenttextview.settext (string); Toast.maketext (Getactivity (), "Fetch data successfully" + String, Toast.length_short). Show (); return view;}}
Summary : Send data method->setarguments (bundle)Receive Data Method->getarguments ()
let's look at the effect:
Second, fragment to the activity value (interface callback mode)layout file As above, let's look at the changes in the Java code:1.mainactivity.java:
Package Com.example.fragementcommunication;import Com.example.fragementcommunication.MyFragment.MyListener; Import Android.app.activity;import Android.app.fragmentmanager;import Android.app.fragmenttransaction;import Android.os.bundle;import Android.view.view;import Android.view.view.onclicklistener;import Android.view.Window; Import Android.widget.button;import Android.widget.edittext;import Android.widget.toast;public class MainActivity Extends Activity implements Mylistener{private EditText mmainactivityet;private Button Msendbutton; @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Requestwindowfeature (window.feature_ No_title); Setcontentview (r.layout.activity_main); Mmainactivityet = (EditText) Findviewbyid (r.id.et_input); Msendbutton = (Button) Findviewbyid (r.id.btn_send); Msendbutton.setonclicklistener (new Onclicklistener () {@ overridepublic void OnClick (View v) {String text = Mmainactivityet.gettext (). toString (); Myfragment myfragment = newMyfragment (); Bundle bundle = new bundle (); Bundle.putstring ("Input", text); myfragment.setarguments (bundle);// Pass Stringfragmentmanager Manager = Getfragmentmanager (); Fragmenttransaction transaction = Manager.begintransaction (); Transaction.add (R.id.rl_fragment, Myfragment, " Myfragment "); Transaction.commit (); Toast.maketext (Mainactivity.this, "Send data to Fragment" + text, Toast.length_short). Show ();});} @Overridepublic Void callback (String back) {Toast.maketext (mainactivity.this, "Get data from fragment" + Back, Toast.length_short). Show ();}}
2.myfragment.java:
Package Com.example.fragementcommunication;import Android.app.activity;import Android.app.fragment;import Android.os.bundle;import Android.view.layoutinflater;import Android.view.view;import Android.view.ViewGroup; Import Android.widget.textview;import Android.widget.toast;public class Myfragment extends Fragment {public MyListener Listener;private String back= "has received data! Thank you ";/** * Transfer value from fragment to activity via interface callback; * @author Administrator * */public interface mylistener{public void Callback (STR ing back); @Overridepublic void Onattach (activity activity) {Super.onattach (activity); listener= (MyListener) Activity;} @Overridepublic View Oncreateview (layoutinflater inflater, ViewGroup container, Bundle savedinstancestate) {View view = I Nflater.inflate (R.layout.fragmet, NULL); TextView Mfragmenttextview = (TextView) View.findviewbyid (r.id.tv_fragment); String string = Getarguments (). getString ("input");//Get Data; Mfragmenttextview.settext (string); Toast.maketext (Getactivity (), "Fetch data successfully" + String, Toast.length_short). Show (); Toast.maketext (Getactivity (), "passing data to activity" + back, Toast.length_short). Show (); Listener.callback (back); return View;}}
Run as follows:
at this point we can see that first mainactivity to fragment 123, and then fragment to the activity "has received the data!" Thank you. "
Favorite Friends Please follow me and my public number! Thanks
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Android Combat Simple Tutorial-52nd gun (communication between fragment and activity)