Data Interaction Between Fragment and Activity in Android development is implemented through callback (source code sharing)

Source: Internet
Author: User

The previous article briefly introduced the use of the callback mechanism of Android. This blog post will focus on the data interaction between Fragment fragments and activity. fragment plays a vital role in Android development, through the official Android api, we can see that fragment has its own lifecycle and relies on the lifecycle of the activity it is bound to. How does the activity interact with fragment, read this blog post.

MainActiivity code

Package com. example. f08_fragment03; import com. example. f08_fragment03.Fragment02.Callback; import android. OS. bundle; import android. app. activity; import android. app. fragmentManager; import android. app. fragmentTransaction; import android. view. menu; import android. view. view; import android. widget. button; import android. widget. editText; public class MainActivity extends Activity {private FragmentManager manager; // create a fragment management private FragmentTransaction transaction; // create a fragment object private EditText editText EditText; private Button button; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); editText = (EditText) this. findViewById (R. id. editText2); button = (Button) this. findViewById (R. id. button1); manager = getFragmentManager (); transaction = manager. beginTransaction (); final Fragment02 fragment02 = new Fragment02 (); transaction. add (R. id. left, fragment02, "left"); transaction. commit (); // The following three methods must be called no matter what features you want to implement. // manager = getFragmentManager (); // transaction = manager. beginTransaction (); // transaction. commit (); submit data button. setOnClickListener (new View. onClickListener () {@ Overridepublic void onClick (View arg0) {// TODO Auto-generated method stub // get the fragment data fragment02.getString (new callback () through the Callback mechanism () {@ Overridepublic void getString (String msg) {// TODO Auto-generated method stubeditText. setText (msg) ;}}) ;}@ Overridepublic boolean onCreateOptionsMenu (Menu menu) {// Inflate the menu; this adds items to the action bar if it is present. getMenuInflater (). inflate (R. menu. main, menu); return true ;}}
Fragment02 code

Package com. example. f08_fragment03; import android. app. activity; import android. app. fragment; import android. OS. bundle; import android. util. log; import android. view. layoutInflater; import android. view. view; import android. view. viewGroup; import android. widget. button; import android. widget. editText; public class Fragment02 extends Fragment {private Button button; private EditText editText; @ Overridepublic void onAttach (Activity activity) {// TODO Auto-generated method stubsuper. onAttach (activity) ;}@ Overridepublic void onCreate (Bundle savedInstanceState) {// TODO Auto-generated method stubsuper. onCreate (savedInstanceState);} // update the UI @ Overridepublic View onCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) in this method) {// TODO Auto-generated method stubView view = inflater. inflate (R. layout. left, null); button = (Button) view. findViewById (R. id. button1); editText = (EditText) view. findViewById (R. id. editText1); button. setOnClickListener (new View. onClickListener () {@ Overridepublic void onClick (View arg0) {// obtain the activity control through the getActivity method, so that the data EditText eText = (EditText) getActivity () can be obtained (). findViewById (R. id. editText2); Log. I ("info", "------->" + eText. getText (). toString (); editText. setText (eText. getText (). toString () ;}}); return view ;}@ Overridepublic void onStart () {// TODO Auto-generated method stubsuper. onStart () ;}@ Overridepublic void onResume () {// TODO Auto-generated method stubsuper. onResume () ;}@ Overridepublic void onPause () {// TODO Auto-generated method stubsuper. onPause () ;}// implement public void getString (Callback callback) {String msg = editText. getText (). toString (); callback. getString (msg);} // create interface public interface Callback {public void getString (String msg );}}

Xml of main

<RelativeLayout xmlns: android = "http://schemas.android.com/apk/res/android" xmlns: tools = "http://schemas.android.com/tools" android: layout_width = "match_parent" android: layout_height = "match_parent" tools: context = ". mainActivity "> <LinearLayout android: id =" @ + id/left "android: layout_width =" 200dp "android: layout_height =" match_parent "android: background =" # cbcbcb "android: orientation = "vertical"> </LinearLayout> <LinearLayout android: id = "@ + id/right" android: layout_width = "200dp" android: layout_height = "match_parent" android: orientation = "vertical"> </LinearLayout> <EditText android: id = "@ + id/editText2" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: layout_alignParentTop = "true" android: layout_marginTop = "14dp" android: layout_toRightOf = "@ + id/left" android: EMS = "10" android: inputType = "textPassword"> <requestFocus/> </EditText> <Button android: id = "@ + id/button1" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: layout_below = "@ + id/editText2" android: layout_toRightOf = "@ + id/left" android: text = "Get Fragment data"/> </RelativeLayout>

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"> <EditText android: id = "@ + id/editText1" android: layout_width = "match_parent" android: layout_height = "wrap_content" android: inputType = "text" android: EMS = "10"> <requestFocus/> </EditText> <Button android: id = "@ + id/button1" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: text = "Get Activity data"/> </LinearLayout>

In this way, you can click the fragment button to display the text of the editText control of the activity in its editText control. Click the activity button to get the data of the Edit Control of fragment!


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.