-2

I am trying to create a memeApp, I created two fragments:

  1. The first fragment has two EditText fields, where the user writes something and a button;
  2. The second fragment has a picture, with two EditText fields, where one is top positioned in the picture, and the second is at bottom.

Fragment class A

public class topFragment extends Fragment {
    private static EditText topText;
    private static EditText botText;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
    Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.top_sectionfragment, container, false);
        topText = (EditText) view.findViewById(R.id.topText);
        botText = (EditText) view.findViewById(R.id.botText);
        final Button buttonChange = (Button) 
        view.findViewById(R.id.buttonChange);

        buttonChange.setOnClickListener(
            new View.OnClickListener() {
                public void onClick(View v) {
                    buttonClicked(v);
                }
            }
        );


        return view;

        }

        public void buttonClicked(View view) {


        } 

    }

Fragment class B

public class bottomfragmentphoto extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
    Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.botfragment,container,false);
        return view;
    }
}

I want to know, how and what can I do, when I write something in EditText from fragment A, to send the data to Fragment B? And all of this I want to do with mainActivity.

TT.
  • 15,774
  • 6
  • 47
  • 88
Boklee
  • 7
  • 1
  • 2

5 Answers5

0

there is no direct communication possible , you can use this route fragment1->activity->fragment2 by using interfaces or use a higher level concept of startActivityForResult concept . There are lot of examples available in online tutorials,its a complex process to handle , so try you hand at it ,then you can ask for help in that by posting your code

Ak9637
  • 990
  • 6
  • 12
0

You can create a bundle in your first fragment, then fill it with your desire data, and pass it as an argument to your second fragment....and there you can read that bundle and retrieve your data.

Let me know if you needed any example

Mohammad Zarei
  • 1,773
  • 14
  • 33
0

Use greenrobot's event Bus libaray. You can register it inside the fragments and when some event happens post it to event bus, It will notify the respected fragment or activity, Here is the link for the library

Add this to gradle

    compile 'org.greenrobot:eventbus:3.0.0'

Then

Register the EventBus to the fragment or activity in onResume and unregister it in onPause

@Override
protected void onResume() {
    super.onResume();
    EventBus.getDefault().register(this);
}

@Override
protected void onStart() {
    super.onStart();
    EventBus.getDefault().unregister(this);
}

Declare a method inside the fragement/activity which will be triggered when event happens

 @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEvent(SomeEvent someevent) {

    }

someevent can be object of any class

Then you can notify about the event using the instance of EventBus anywhere

EventBus eventBus = EventBus.getDefault();
eventBus.post(someEventHasHappend)

This will notify the onEvent()

Hope this will help you

Zeeshan Shabbir
  • 6,704
  • 4
  • 38
  • 74
0

Beside other suggestions, also you can use RxAndroid. Create an Observable in the first fragment and pass it to the second one, then in the second one you can subscribe an observer to it.

see https://code.tutsplus.com/tutorials/getting-started-with-reactivex-on-android--cms-24387

hadilq
  • 1,023
  • 11
  • 25
0

Afaik, there are 2 ways to communicate between 2 fragments:

  1. Through the associated Activity
    In Communicating with Other Fragments official documentation, it says Two Fragments should never communicate directly.
    To allow a Fragment to communicate up to its Activity, you can define an interface in the Fragment class and implement it within the Activity. The Fragment captures the interface implementation during its onAttach() lifecycle method and can then call the Interface methods in order to communicate with the Activity. Read the documentation for details.

  2. Using Event Bus
    You can use Event Bus architecture like EventBus as a tools for communication between 2 fragments. But I don't recommend this, because of the nature of fragments where it must be attached to an Activity and coupled to the Activity. You can read more at Communicating with an Event Bus

ישו אוהב אותך
  • 28,609
  • 11
  • 78
  • 96