0

I have another one question about transfering data from Activity to Fragment.

In my activity I have next situation: one part of UI is situated in Activity, and another (more dynamic part) is situated in Fragment.

The data which I need to populate my UI elements in Activity and Fragment is on server. To get that data, I am sending request to server in Acvitity's onCreate() method. In Activity's callback method: void onDataLoaded(List<MyObject> dataList) I get data from server. And in this method I am creating my Fragment and setup data to it. I am passing data to it through the Bundle object duding creation. Everything is ok with this. But the issue is in next: on network reconnect I need to load data from server to be sure that all data is up to date. And of course I need to reinitialize data in Activity and Fragment. But I don't want to fully RE-CREATE fragment. I want just to setup new data to it's fields. How can I do that properly? Is it a good way to to keep reference in my Activity to that Fragment and call some public method: myFragment.SetMyCustomData(List<MyObject> dataList) ? I understand that the best way to load data from server in my fragments onCreate() method, but I can't split it into two API calls and I need that data in Activity as well. Thanks.

yozhik
  • 4,644
  • 14
  • 65
  • 98
  • 1
    You can use the command (bus) pattern. https://github.com/greenrobot/EventBus – Drilon Blakqori Apr 23 '18 at 08:22
  • Thanks, but I want to get some native code, not framework. – yozhik Apr 23 '18 at 08:38
  • 1
    It's not a framework, it's a lightweight library that allows you to apply the command pattern. Communication between activities and fragments is very tricky, so most of the android developers out there use this pattern to ensure loose and easy communication. – Drilon Blakqori Apr 23 '18 at 13:06

1 Answers1

0

Well this is not a transfering problem . Anyway you can use Event Bus for easier communication from Activity to Fragment and vice-versa. It is very easy to use . And as for your problem I belive you are not doing what you need in the right time .

https://github.com/greenrobot/EventBus