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
  • 42,072
  • 54
  • 195
  • 297

2 Answers2

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,776
  • 2
  • 21
  • 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,623
  • 3
  • 48
  • 80