简体   繁体   中英

How to pass data from activity to Fragment when pressed back in activity ? - android

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?

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

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM