How to pass data between fragments

后端 未结 13 2800
醉话见心
醉话见心 2020-11-22 07:03

Im trying to pass data between two fragmens in my program. Its just a simple string that is stored in the List. The List is made public in fragments A, and when the user cli

13条回答
  •  死守一世寂寞
    2020-11-22 07:20

    Basically Implement the interface to communicate between Activity and fragment.

    1) Main activty

    public class MainActivity extends Activity implements SendFragment.StartCommunication
    {
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    }
    
    @Override
    public void setComm(String msg) {
    // TODO Auto-generated method stub
    DisplayFragment mDisplayFragment = (DisplayFragment)getFragmentManager().findFragmentById(R.id.fragment2);
    if(mDisplayFragment != null && mDisplayFragment.isInLayout())
    {
    mDisplayFragment.setText(msg);
    }
    else
    {
    Toast.makeText(this, "Error Sending Message", Toast.LENGTH_SHORT).show();
    }
    }
    }
    

    2) sender fragment (fragment-to-Activity)

    public class SendFragment extends Fragment
    {
    StartCommunication mStartCommunicationListner;
    String msg = "hi";
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
    Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    View mView = (View) inflater.inflate(R.layout.send_fragment, container);
    final EditText mEditText = (EditText)mView.findViewById(R.id.editText1);
    Button mButton = (Button) mView.findViewById(R.id.button1);
    mButton.setOnClickListener(new OnClickListener() {
    
    @Override
    public void onClick(View arg0) {
    // TODO Auto-generated method stub
    msg = mEditText.getText().toString();
    sendMessage();
    }
    });
    return mView;
    }
    
    interface StartCommunication
    {
    public void setComm(String msg);
    }
    
    @Override
    public void onAttach(Activity activity) {
    // TODO Auto-generated method stub
    super.onAttach(activity);
    if(activity instanceof StartCommunication)
    {
    mStartCommunicationListner = (StartCommunication)activity;
    }
    else
    throw new ClassCastException();
    
    }
    
    public void sendMessage()
    {
    mStartCommunicationListner.setComm(msg);
    }
    
    }
    

    3) receiver fragment (Activity-to-fragment)

        public class DisplayFragment extends Fragment
    {
    View mView;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
    Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    mView = (View) inflater.inflate(R.layout.display_frgmt_layout, container);
    return mView;
    }
    
    void setText(String msg)
    {
    TextView mTextView = (TextView) mView.findViewById(R.id.textView1);
    mTextView.setText(msg);
    }
    
    }
    

    I used this link for the same solution, I hope somebody will find it usefull. Very simple and basic example.

    http://infobloggall.com/2014/06/22/communication-between-activity-and-fragments/

提交回复
热议问题