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.
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);
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.
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.
Related
I am making an app that pulls from a mySQL database. I have a main activity which holds a drawer with a bunch of fragments. If I wanted to make a query and save information in main activity in an array or something, would it be accessible in all of my fragments? I've had trouble trying to send data from fragments, and just read that fragments in the same activity all share the same data. Could someone confirm that if I made an array in mainActivity, that the array would be accessable and mutable by the fragments? (this is my first post, sorry if it's bad)
Why don't you move the array from main activity and create repo which will provide data on request for fragments and main activity. Change in orientation can cause the data in main activity to be wiped out if you are not handling the orientation.
The best way I would recommend is to use view model for the main activity and use that for fragments.
Share data between fragments
Hope it helped. Happy coding :)
In my app I get some data from server in json. Then I fetch fields from json and fill my RecyclerView list with custom adapter. This list is filled with data about jobs. When I click on a job of my recyclerView I move from hosting fragment to dialogfragment for viewing it. This dialogFragment contains homesUp button after pressing which I return to hosting activity. At this dialogFragment I have button which send request to server about this job for adding it to another DB at server.
But when I press this button and send request to the server then after returning to hosting activity I see that data of selected isn't changed. I have several ways of solving this problem:
Add to my Singleton class variable of ArrayList which will be filled by data of shown job and then I will check whether my Arraylist from Singleton contains this id
another method - notifyItemChanged()
All these methods don't work because my list is filled by old data and I only dismiss dialogFragment and return to fragment which contains the list with old data.
Only one way - send request to server for filling the list again.
So, I need your help or useful advice :)
You need to broadcast changed data from dialog fragment to previous screen. For doing this can use LocalBroadcastManager or rxjava or even live data.
Or create a pagedlistadapter from paging library released in android architecture complement and load data directly from query.
Reference: https://developer.android.com/topic/libraries/architecture/paging
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.
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
I have a viewpager fragment setup, and I have an AsyncTask run at the start of the activity to load some data from a JSON get (the data it pulls only needs to be done once, and all fragments will use it). The problem I'm having is I want to take this data from the AsyncTask, and have it be the data that all of the textviews pull from in the fragments.
What would be the best way to load data from an AsyncTask and then have the fragments reference that data. You cant just have the views reference the data from the get-go as it Nullpointers, and I'd really like to avoid having to reference every single textview in the OnPostExecute of the AsyncTask if I had it in the Parent Activity.
Just decouple the storage of the data from the view(s). In that way it will be easyer to handle the download of the data and to show it in the various fragments once the get is completed.
The basic idea is: launch the download in a background thread decoupled from the activity (service or intent service), then update the data a storage (sqllite if it is complex, shared preferences or even a singleton object, even though I don't like the latter approach).
Once the get is performed, inform the fragments that the data is available. Still, you have a lot of options here. If you decided to host the thread / asynctask inside a service, you can bind a callback to it and then notify all the fragments interested, you can use a (local) broadcast message that you can intercept using local broadcast receivers, or you can even use a message bus such as otto.
Finally, I really recommend to use an intent service to perform the async job. It's the easiest way to do any one shot operations without having to deal with service creation and/or with activity configuration changes.
Not sure if this is the "best" way, but what comes to my mind first is the famous Observer pattern.
Create an interface called Observer with a single method called
notify()
Implement this interface in all Fragments that want to be notified when the data is ready
Hold a List of Observer objects inside your AsyncTask
In onPostExecute(), traverse this List and call notify() on every Observer, passing the appropriate data
React on the notification inside your Fragments given the data
This a very rough description of this solution, but I hope you get the idea.
Lets say I have 2 activities:
A: A ListView displaying articles titles. Data is fetched from a web server and converted from XML to a list of ArticleSummary. Only user titles and id are returned by the server. Click on a title starts activity B.
B: A form to edit an article. Article is fetched from server. When the user hits OK, modifications are sent to the server and activity closed.
When the user go back to activity A, I would like to update the article title without any additional web request.
I was thinking about the following solution:
When article is modified, send a broadcast event with article id and new attributes values.
Listen for this event on activity A
Update the ArticleSummary object
notify data changed on ListView
Is there a better approach ?
If you want to have a shared data model between different Activities, you can place it in an extension of the Application class. Or, you can use a singleton. Just reload the data from the shared location when the ListView activity is restarted.
As Fredley alluded to, if you have are communicating with a server you should be sure to do so in a separate background thread.
you can also use startActivityForResult() to launch activity and managed returned data.
Check the "Returning a Result from a Screen" section in the part below.
http://developer.android.com/guide/appendix/faq/commontasks.html