Android practice simple tutorial-52nd guns (Communication Between Fragment and Activity), androidfragment

Source: Internet
Author: User
Tags string back

Android practice simple tutorial-52nd guns (Communication Between Fragment and Activity), androidfragment

The use of Fragment makes our applications more flexible to adapt to various Android devices, but many friends should be unfamiliar with the communication between Fragment and Activity, next we will use an instance to see how to implement it.

1. Activity-> Fragment transmits data 1. 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:orientation="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 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 (), "data retrieved successfully" + string, Toast. LENGTH_SHORT ). show (); return view ;}}

Summary: Data sending method-> setArguments (bundle) method of receiving data-> getArguments ()
Let's take a look at the effect:


2. The layout file of the Fragment value (interface callback method) passed to the Activity is the same as above. Let's take a look at the changes to 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 = 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 () ;}}) ;}@ 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 = "received data! Thank you. ";/*** send a value from Fragment to activity through interface callback; * @ author Administrator **/public interface MyListener {public void callback (String back );} @ Overridepublic void onAttach (Activity activity) {super. onAttach (activity); listener = (MyListener) activity;} @ Overridepublic View onCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {View 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 (), "data retrieved successfully" + string, Toast. LENGTH_SHORT ). show (); Toast. makeText (getActivity (), "pass data to Activity" + back, Toast. LENGTH_SHORT ). show (); listener. callback (back); return view ;}}

Run the following command:

At this time, we can see that MainActivity first transmitted 123 to Fragment, and then Fragment passed "received data to Activity! Thank you.
If you like it, please follow me and my public account! Thank you.





Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

Related Article

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.