简体   繁体   中英

sending multiple data from one fragment to another fragment

In My app..one mainActivity..two fragment like fragment A and FragmentB.. I Created..And I added two editText in FragmentA..I want to send this editText data to FramentB..and reuse itin..FragmentB..how to do this... I should use interface concept..or..is their any another concept..

    public class FragmentA extends Fragment  {
      Button nextt;
       EditText number;

      EditText alpha;
       }
         @Override
         public void onStart() {
          super.onStart();
           fm = ((MainActivity) context).getFragmentManager();
      }

      @Override
       public void onClick(View view) {
          int viewId = view.getId();
          FragmentTransaction ft;
          ft = fm.beginTransaction();
         FragmentB fragmentB = new FragmentB();
          ft.replace(R.id.frame_content, fragmentB);
          ft.addToBackStack(null);
           ft.commit();
     }

     @Override
       public View onCreateView(LayoutInflater inflater, ViewGroup container,     Bundle savedInstanceState) {
           View view = null;

            view = inflater.inflate(R.layout.fragment_a, container, false);

           nextt = (Button) view.findViewById(R.id.Button1);
             nextt.setOnClickListener(this);

            number = (EditText) view.findViewById(R.id.swTypeE);

           alpha = (EditText) view.findViewById(R.id.numE);
         }
   }
   }

//Put the value

YourNewFragment ldf = new YourNewFragment ();
Bundle args = new Bundle();
args.putString("YourKey", "YourValue");
ldf.setArguments(args);

//Inflate the fragment

getFragmentManager().beginTransaction().add(R.id.container, ldf).commit();

In onCreateView of the new Fragment:

//Retrieve the value

String value = getArguments().getString("YourKey");
 @Override
 public void onClick(View view) {
 int viewId = view.getId();
 FragmentTransaction ft;
 ft = fm.beginTransaction();
 Bundle bundle = new Bundle();
 bundle.putInt("yourkey",viewId);
 FragmentB fragmentB = new FragmentB();
 fragmentB.setArguments(bundle);
 ft.replace(R.id.frame_content, fragmentB);
 ft.addToBackStack(null);
 ft.commit();}

Get Value in fragmentB

String value = getArguments().getString("yourkey");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM