0

What i have

  • I have an activity
  • MainActivity.java here in this activity I have a fragment(MainFragment.java)
  • In MainFragment.java I have two child fragments ChildOne.java and ChildTwo.java
  • I have a editText in ChildOne.java

What I am trying to do:

  • I need to get the value in the editText of ChildOne.java from ChildTwo.java
  • how to achieve this !
Devrath
  • 39,949
  • 51
  • 178
  • 266

3 Answers3

0

You define an interface in your Fragment A, and let your Activity implement that Interface. Now you can call the interface method in your Fragment, and your Activity will receive the event.

check this link Basic Communication between two fragments

Hope this answer is helpful friend :)

Community
  • 1
  • 1
Prince Thomas
  • 990
  • 7
  • 10
0

You can achieve this in multiple ways, but I'll give you one supposing that both Fragments are displayed at the same time.

In your ChildTwo use getParentFragment to get MainFragment.

MainFragment parent = (MainFragment) getParentFragment();
String edit_text_value = parent.getChildOneText();

In your MainFragment you need a reference object to your ChildOne fragment.

public String getChildOneText()
{
     return mFirstFragment.getEditTextValue();
}

Finally in your ChildOne fragment create a method to return your EditText.

public String getEditTextValue()
{
   return my_edit.getText().toString();
}

Hope it helps!

zozelfelfo
  • 3,716
  • 2
  • 19
  • 35
0

You may create one function in mainActivity and a variable that holds the value in there. And call that function when you want to save the data of editText, And can create a function to get the data in MainActivity as well. Call those functions like this -

((Home) getActivity()).shareData("example string"); ((Home) getActivity()).receiveData("example string");

May check this answer and this one

Community
  • 1
  • 1
Darpan
  • 5,413
  • 3
  • 46
  • 80