Send data from Activity to Fragment already created

sergio picture sergio · Jul 7, 2017 · Viewed 13.5k times · Source

I only have found information about how to create a Fragment sending some data to it, but only in its instantiation with the constructor.

But I want to know if it is possible to send some data (for instance, two Double objects) to a Fragment from an Activity without having to create a new instance of the Fragment.

A Fragment that has been previously created.

Answer

Will Tang picture Will Tang · Jul 7, 2017

Just add a method in Fragment which you want to receive arguments, then invoke the method in Activity.

Activity's Code:

Activity's Code

Fragment's Code:

Fragment's Code