How to pass a value from one Fragment to another i

2019-01-11 14:26发布

问题:

I am a newbie to Fragments. I want to pass a String value from one Fragment to another. how to do this? I have called my Fragments in the following way. please guide me step by step.

String cid = id.getText().toString();
Fragment fr = new FriendFragment();
android.app.FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.id.content_frame, fr);
ft.commit(); 

回答1:

You can do something like below,

 String cid=id.getText().toString();
 Fragment fr=new friendfragment();
 FragmentManager fm=getFragmentManager();
 android.app.FragmentTransaction ft=fm.beginTransaction();
 Bundle args = new Bundle();
 args.putString("CID", cid);
 fr.setArguments(args);
 ft.replace(R.id.content_frame, fr);
 ft.commit(); 

To receive the data do the following,

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    String strtext = getArguments().getString("CID");    
    return inflater.inflate(R.layout.fragment, container, false);
}


回答2:

If you want to send data from fragment to activity you may use an interface.

But when you want to send data from fragment to another fragment it get's complicated. You would want to send data to activity and then to the other fragment.

I use EventBus to solve this problem. How it works.

  1. Create an event-E.
  2. From Fragment-A register for events.
  3. From fragment-B publish event-E with data you want to pass.
  4. You would get the data in onEvent() method you wrote in Fragment-A.

That's it. No need to write your own interfaces.

You may use eventbus for communications from background service or threads to activity also.

Checkout the EventBus HowTo and Repository also.



回答3:

Fragment to Fragment communication must be done through the parent Activity.

FragmentToSendData

interface <interfaceName>{
    void <abstract method>(String str);
}

@Override
public void onAttach(Activity activity) {
        super.onAttach(activity);
       try{
           <instance of the interface> = (<interface>)getActivity();
       }catch (ClassCastException e) {
           throw new ClassCastException(getActivity().toString()
                   + " must implement <interface>");
       }

    }

ActivityWithBothFragments (this can be through ViewPager, or use the id of your fragment, just use findFragmentById() )

@Override <abstract method from frag1>(String str){
  FragmentToReceiveData fragment2 =  (FragmentToReceiveData)getSupportFragmentManager().findFragmentByTag(ViewPagerAdapter.getFragmentTag(1));                                                                                                                                                

    fragment2.getStringFromFrag1(String str);
}

FragmentToReceiveData

public void getStringFromFrag1(String str){
  <textview>.setText("str");

}


回答4:

try this :

- send data : 

        Bundle arg = new Bundle();
        arg.putInt("ID", "12");  //arg.putInt("KEY", "VALUE")
        arg.putString("NAME","Jaydeep");//arg.putString("KEY","VALUE")

        YourFragment fragment = new YourFragment();
        fragment.setArguments(arg);

        fragmentManager.beginTransaction().add("PUT YOUR FRAM ID", fragment, fragment.getClass().getName())
        .addToBackStack(null)
        .commit();

- receive data : 

        Bundle bundle = getIntent().getExtras();
        bundle.getInt("ID");
        bundle.getString("NAME");

        Log.e("Receive data : " ,"\nID - "+bundle.getInt("ID")+"\nNAME - "+bundle.getString("NAME"));