2

We are using Retrofit and Activeandroid for my project.

Currently we are facing an issue. The pattern which we follow in our project is, We get data from server and save it into local database and after data saved we call routine which fetches data from database and populate UI this all happens in single Activity..

Now we have an activity which makes 3 server request, and due to which the amount of code in activity increased.

We are trying to reduce Activity code by creating fragment for the activity and giving responsibility of fetching data and displaying data to Fragment. Rest call will be made by activity. Now once the data is loaded from all the 3 request we need to inform fragment about data is loaded, what is the best way for this.

And is it even possible to send data to fragment once it is loaded.. or the approach we are following is not correct..

Please guide us on this..

Edit1 I read about EventBus. Can event bus solve this problem or it will effect the efficiency.

2
  • does your fragment only have async task or UI also? Sep 22, 2015 at 6:18
  • Our fragment only fetch data from database and populate UI. i guess it is more of sync task.. Sep 22, 2015 at 6:28

3 Answers 3

2

If you are storing those Fragment instances in your Activity then it will be much easier.

1) Create loadNewdata(DataType data) method in your Fragment.

2) Pass the data into the Fragment after getting response from server in your Activity

((YourFragment)fragment).loadNewdata(yourData);
0
0

I would suggest to use Otto.

By this you do not need to store your data in local db(if you don't want it).Just simply post your responce whenever you received from rest API. As Otto also provides the functionality of sharing custom objects between fragments/Activity. It also helps you to make your code modular. You will find a working example here & here.

0

You can use Interfaces for this and send data to activity.when response come back pass reference of to show which fragment data needs to be populate the UI.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.