0

I am currently using Retrofit and making API calls in Activity and to pass the Data from activity to fragment using Shared Preferences or using Constructor of RecyclerView adapter to send the data from activity to recyclerview

I would like to know what are other ways of sharing the data between activity and fragment which will have significant improvements on performance?

Swanand Keskar
  • 1,023
  • 1
  • 13
  • 27
  • Use a design pattern like MVP / MVC etc. Activities and fragments should not be responsible for handling API calls. Their responsibility is to show information / hind information / handle user interactions. In this way you will keep them as clean as possible! When you receive the data from the API calls, just use interfaces to pass it to the corresponding Activity or Fragment. – Todor Kostov Dec 15 '16 at 07:14
  • @TodorKostov is there any sample code available with implementation ? – Swanand Keskar Dec 15 '16 at 12:06
  • 1
    There are hundreds of articles and git repos. Check here http://stackoverflow.com/questions/41160019/how-can-i-update-activity-fragment-ui-from-retrofit-onresponse/41160263#41160263 or here https://github.com/konmik/konmik.github.io/wiki/Introduction-to-Model-View-Presenter-on-Android or here https://github.com/antoniolg/androidmvp Found them after 10 sec. search with Google... – Todor Kostov Dec 15 '16 at 12:13

1 Answers1

1

your can pass data from activity to fragment through bundle using setArgument method in fragment and pass bundle which takes your data like below

   YourFragment fragment = new YourFragment();
    Bundle bundle = new Bundle();
    bundle.putString(key, value);
    fragment.setArguments(bundle);

And inside your fragment do getArguments() to get back your bundle

sohan shetty
  • 289
  • 1
  • 16