Basic communication between two fragments

前端 未结 10 2170
我寻月下人不归
我寻月下人不归 2020-11-22 05:00

I have one activity - MainActivity. Within this activity I have two fragments, both of which I created declaratively within the xml.

I am trying to pas

10条回答
  •  攒了一身酷
    2020-11-22 05:29

    Consider my 2 fragments A and B, and Suppose I need to pass data from B to A.

    Then create an interface in B, and pass the data to the Main Activity. There create another interface and pass data to fragment A.

    Sharing a small example:

    Fragment A looks like

    public class FragmentA extends Fragment implements InterfaceDataCommunicatorFromActivity {
    public InterfaceDataCommunicatorFromActivity interfaceDataCommunicatorFromActivity;
    String data;
    
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        return super.onCreateView(inflater, container, savedInstanceState);
    }
    
    @Override
    public void updateData(String data) {
        // TODO Auto-generated method stub
        this.data = data;
        //data is updated here which is from fragment B
    }
    
    @Override
    public void onAttach(Activity activity) {
        // TODO Auto-generated method stub
        super.onAttach(activity);
        try {
            interfaceDataCommunicatorFromActivity = (InterfaceDataCommunicatorFromActivity) activity;
        } catch (ClassCastException e) {
            throw new ClassCastException(activity.toString()
                    + " must implement TextClicked");
        }
    
    }
    
    }
    

    FragmentB looks like

    class FragmentB extends Fragment {
    public InterfaceDataCommunicator interfaceDataCommunicator;
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
    
        // call this inorder to send Data to interface
        interfaceDataCommunicator.updateData("data");
    }
    
    public interface InterfaceDataCommunicator {
        public void updateData(String data);
    }
    
    @Override
    public void onAttach(Activity activity) {
        // TODO Auto-generated method stub
        super.onAttach(activity);
        try {
            interfaceDataCommunicator = (InterfaceDataCommunicator) activity;
        } catch (ClassCastException e) {
            throw new ClassCastException(activity.toString()
                    + " must implement TextClicked");
        }
    
    }
    
    }
    

    Main Activity is

    public class MainActivity extends Activity implements InterfaceDataCommunicator {
    public InterfaceDataCommunicatorFromActivity interfaceDataCommunicatorFromActivity;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
    
    @Override
    public 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;
    }
    
    @Override
    public void updateData(String data) {
        // TODO Auto-generated method stub
        interfaceDataCommunicatorFromActivity.updateData(data);
    
    }
    
    public interface InterfaceDataCommunicatorFromActivity {
        public void updateData(String data);
    }
    
    }
    

提交回复
热议问题