Android Fragment Series Study Notes 6

Source: Internet
Author: User


1. Data Interaction Between Fragment and Fragment
(1) ask the question: in what scenarios do Fragment and Fragment require data interaction?
(2) image description:

Scenario Description:Assume that the Fragment on the left has an input box and a button. The Fragment on the right has an input box and a button. You can obtain the value of EditText In the first Fragment when you click the button in Fragment on the right.

(3) Implementation ideas

The first thing to consider is how to obtain the reference of Fragment on the right?

We can consider using FragmentManager. Because all Fragment is managed by the same FragmentManager in the Activity, after the Fragment on the left gets the reference of the FragmentManager object, next, you can find all the Fragment objects managed by the FragmentManager object, so you can operate any of the Fragment objects.

(4) instance code:

Package com. example. fragment; import android. OS. bundle; import android. support. v4.app. fragment; import android. support. v4.app. fragmentManager; import android. view. layoutInflater; import android. view. view; import android. view. viewGroup; import android. widget. button; import android. widget. editText; import android. widget. toast; import com. example. android_fragment_trans.R;/*** file name: com. example. fragment. leftFragment * Author: Xiong jie * Date: 13-12-13 * Time: * Development Tool: IntelliJ IDEA 12.0.4 * development language: Java, Android * development framework: * version: v0.1 *Every time you write a Fragment, You need to consciously write these three methods.*

*/Public class LeftFragment extends Fragment {private Button btLeft; private FragmentManager fragmentManager; private RightFragment rightFragment; private EditText etRight; /*** this method instantiates the variable * @ param savedInstanceState */@ Override public void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); fragmentManager = getFragmentManager ();}/*** this method is used to load View * @ param inflater * @ pa Ram container * @ param savedInstanceState * @ return * click the Fragment Button on the left to obtain the value in the Fragment text input box on the right * first, you must consider how to obtain the reference of Fragment on the right. * There is an idea that all Fragment is managed by the same FragmentManager in the Activity. * Can the FragmentManager object be obtained in Fragment on the left? ** In addition, if Fragment is nested with Fragment and there are still several children in a Fragment, these children are equal. * GetChildFragmentManager () obtains the current Fragment instead of Activity */@ Override public View onCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {View view = inflater. inflate (R. layout. left_fragment, null); btLeft = (Button) view. findViewById (R. id. bt_left); btLeft. setOnClickListener (onClickListener); return view;}/*** indicates that data is submitted when Fragment is removed */@ Override public void onPa Use () {super. onPause () ;}/ *** when you click the Fragment button on the left, you can use the FragemntManager () management class to find any Fragment through findFragmentByTag. * If you can find the Fragment by using the findFragmentByTag () method, you can ensure that the Fragment interacts with all Fragment at the same level. */Private View. onClickListener onClickListener = new View. onClickListener () {@ Override public void onClick (View view) {rightFragment = (RightFragment) fragmentManager. findFragmentByTag ("right"); etRight = (EditText) rightFragment. getView (). findViewById (R. id. et_right); String etRightValue = etRight. getText (). toString (); Toast. makeText (getActivity (), "right ==>>" + etRightValue, Toast. LENGTH_LONG ). show ();}};}


Package com. example. fragment; import android. OS. bundle; import android. support. v4.app. fragment; import android. support. v4.app. fragmentManager; import android. view. layoutInflater; import android. view. view; import android. view. viewGroup; import android. widget. button; import android. widget. editText; import android. widget. toast; import com. example. android_fragment_trans.R;/*** file name: com. example. fragment. rightFragment * by: Xiong jie * Date: 13-12-13 * Time: 06:13 * Development Tool: IntelliJ IDEA 12.0.4 * development language: Java, Android * development framework: * version: v0.1 *Description*

*/Public class RightFragment extends Fragment {private Button btRight; private FragmentManager fragmentManager; private LeftFragment leftFragment; private EditText etLeft; @ Override public void onCreate (Bundle entity) {super. onCreate (savedInstanceState); fragmentManager = getFragmentManager ();} @ Override public View onCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {View view = inflater. inflate (R. layout. right_fragment, null); btRight = (Button) view. findViewById (R. id. bt_right); btRight. setOnClickListener (onClickListener); return view ;}@ Override public void onPause () {super. onPause ();} private View. onClickListener onClickListener = new View. onClickListener () {@ Override public void onClick (View view) {leftFragment = (LeftFragment) fragmentManager. findFragmentByTag ("left"); etLeft = (EditText) leftFragment. getView (). findViewById (R. id. et_left); String etRightValue = etLeft. getText (). toString (); Toast. makeText (getActivity (), "left ==>>" + etRightValue, Toast. LENGTH_LONG ). show ();}};}

Package com. example. android_fragment_trans; import android. OS. bundle; import android. support. v4.app. fragmentActivity; import android. support. v4.app. fragmentManager; import android. support. v4.app. fragmentTransaction; import com. example. fragment. leftFragment; import com. example. fragment. rightFragment; public class MyActivity extends FragmentActivity {private FragmentManager fragmentManager; private Fragmen TTransaction fragmentTransaction;/*** Add the Fragment to the Activity during Activity initialization * @ param savedInstanceState */@ Override public void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. main); fragmentManager = getSupportFragmentManager (); fragmentTransaction = fragmentManager. beginTransaction (); LeftFragment leftFragment = new LeftFragment (); RightFragm Ent rightFragment = new RightFragment ();/*** fragmentTransaction. add () * fragmentTransaction. replace () * What are the differences between the two methods? * The add () method adds Fragment to the Activity. * The replace () method removes the previous Fragment from the Activity */fragmentTransaction. add (R. id. left, leftFragment, "left"); fragmentTransaction. add (R. id. right, rightFragment, "right"); fragmentTransaction. commit ();}}

Ii. Deep topic Introduction

Fragment is nested with Fragment. How can we interact with nested Fragment in Fragment?


That is to say, how does the Fragment on the left implement data interaction with the Fragment on the right?

Implementation ideas:

First, you need to find the Fragment on the right through the FragmentManager management class, and then find the Fragment in the ChildFragmentManager () management class on the right.

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.