1

I have one fragment and one activity, Fragment, and Activity.

In Fragment, I have a Button. When the button is clicked Activity is started.

In Activity, I have an EditText.

I want to display the data retrieved from EditText in Fragment in the TextView when the back is pressed from Activity.

can someone help me with the code to make this work?

jps
  • 20,041
  • 15
  • 75
  • 79
  • 1
    show your code what you done in fragment and activity – Raza Jul 14 '20 at 05:50
  • This link can help you https://stackoverflow.com/questions/32962022/how-to-send-data-from-activity-to-fragment-when-user-clicks-on-back-button-in-an – Elham Dabiri Jul 14 '20 at 06:11

1 Answers1

1

From the Fragment, have a public static variable (String ) so :

public static  String my_text="";

then on the ativity onBackPressed(),set the value of the public static variable like so :

EditText edt;
@Override
public void onBackPressed() {
Yourfragment.my_text=edt.getText.toString();//Yourfragment IS THE NAME OF YOUR FRAGMENT WHERE YOU DECLARED my_text

}
Code Demon
  • 1,256
  • 1
  • 9
  • 15