I am trying to design an Android app which:
grabs JSON data from an HTTP link
iterates through the data and forms an ArrayList of my Object.
Now the HomeActivity extends an ActionBarActivity, which implements TabListener.
It has 2 tabs with a Fragment in each. Fragment 1 is going to hold a listView from the JSON data. Fragment 2 is going to show a Google Map with markers based on the same JSON data.
Now, I'm just wondering what is the best approach to use this AsyncTask.
Should I place it in the Activity and then use interfaces to pass that ArrayList to both the Fragments?
Or ... how should I do this? Thanks! Some tips on caching would also help.
Yes, if your fragments are both using the data, it seems reasonable to put your AsyncTask in the activity. If the AsyncTask is a non-static inner class of the Activity, then in your postExecute you can fetch pointers to your fragments from your activity, and then call methods on the fragments to give them the new data (or tell them that you have stored it somewhere -- however you've implemented it).
Related
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 an Activity, and three tab each with it's own Fragments. I am loading video information from JSON into a list in Activity.
I read URL of video from list and display it in upper half of Activity layout, I have three Fragments in bottom half, the first Fragment shows info about video, second, related video, and third comments.
Problem is: I want to send the list from Activity to 3 Fragments in tabs.
I did lots of ways, using interfaces, callback,... but before fetching JSON data from url, Fragments starts and when this happens the empty list want to be set to TextViews, EditTexts, ImageViews,... in Fragment and then application stops.
How to get data from JSON and then load Fragments? Or
How to simultaneously load data to Activity itself and all Fragments? Or
I can fetch that JSON in activity and 3 Fragments separately, but I don't want to do that!
Please give me a tutorial or an article to learn this subject.
Register callback listeners in your fragments.
Load JSON data in your activity and set the corresponding data to static variables.
Now trigger callback listeners in fragments when you set values in static variables after parsing JSON.
In your callback methods in fragments set the data from static variables.
Update:
You can also pass your data in callback listeners methods to fragments instead of using static variable.
The best approch is to achieve it through interface and put extra but if some how you are not succed you can use below methods:
1.You can use event bus for integrating this funtionality
https://github.com/greenrobot/EventBus
secondly you can do this by creating public getter setter method on parent activity and calling them on fragment by parent activity type context/object.
I am working on a Android app which have 5 fragments and some java classes.
I have to be able to read and edit an arraylist containing pojo's from across these fragments and classes. For example updating from the internet and then updating recyclerView in one of the fragments or sorting the objects in a recyclerView in one fragment and have those changes updated in the recyclerView in another fragment.
I have been looking at notifyDatasetChanged, but cannot get it right, when starting an update in the background and then wants it to update onSucceed in the active fragment.
I have been looking on RxJava with the Arraylist as observable, but once again I ran into problems when I wanted to subscribe from multiple fragments.
And of course I did a arraylist in a singleton, but I am pretty sure that is bad coding :-)
I would put the data that is going to be accessed by all of the fragments in a Service. Each Fragment can bind to the service to retrieve a reference to the data and to register a listener (you will have to make a custom one to handle the events that you are interested in) that will tell each Fragment to update its own view. Each Fragment would implement its own Adapter that would wrap the shared data that lives in the Service.
First I explain the structure of my app. To be precise MainActivity of my app contains Viewpager with two fragments. Each fragment contains a listview. The data inside listview is being populated from web service call.
This web service call is actually done in activity not in the fragments through AsyncTaskLoader which loads data for both listviews and saves the response in two Application variables (Lists of data for both listviews). On finish of loader , i just do viewpager.notifydatasetchanged() (which reloads the fragments , thus sets the data in listview).
As the fragments in viewpager are initialiased by viewpager's adapter no by the activity directly, I found this approach better.
Options I had, but rejected (pls correct me If I could utilise them)
Asynctask to call webservice (headache if handling rotations)
IntentService (updation of UI)
Is there any other approach to load data and handles the fragments?
I have one FragmentActivity that holds ViewPager in which I trough custom FragmentPagerAdapter create 4 child Fragments. Fragments contain ListView in which I show data. I make a call to the web API in FragmentActivity when the response is finished; I want to notify all fragments that data is ready and fill the adapter with that data. Note that data is not available at the time of Fragment initialization.
I could save a reference to fragment when creating it and call “myFragment.hereIsYourData(Object data)”, but the reference get changed after recreating Activity when killing app for low memory. (If I’m not mistaking + I read that keeping a reference to fragment in Activity is a bad thing)
Just for clarification I can’t use “findFragmentById(id)” because ViewPager does’t expose that. I could use the “getFragmentTag” hack explained here https://stackoverflow.com/a/9744146/1025364 but I don’t want to : D.
Then I have tried to ask for data from Fragment onResume method trough interface to Activity (like explained in Developer guide), but the data is not reedy at that moment. Should I ask for data in some sort of a loop until its ready?
What is the best approach in situation like that?