Data loading in multiple fragments in ViewPager - android

I have Activity with ViewPager that holds 3 tabs of Fragments. Each of the fragments has its own URL where it needs to send request and fetch data in JSON format that is shown in Lists in each Fragment.
I am fetching the data in onCreateView and then populating the data in the ListView.
However the ViewPager simultaneously calls onCreateView of both adjacent Fragments due to which the progress bars of multiple Tabs clash and if there is exception in getting data in any one tab it clashes with other tab.
Should the better approach be that i create Objects of Fragments in my Activity and load data in activity and pass on the data to the Fragments as
Fragment one=FragmentOne.getInstance();
Fragment two=FragmentTwo.getInstance();
and after obtaining data ->
one.setData(Data)
two.setData(Data)
If anyone can guide how to do the same.
Thanks

Related

Android : Handling loading of data in fragments inside viewpager

I have a Viewpager Activity with three fragments inside it. The activity has an filter with list of filter options. When activity is started the filter options list is fetched from the server and then the first filter is selected by default.
Fragment A ViewModel --> contains getXList(selectedFilterOption)
Fragment B ViewModel --> contains getYList(selectedFilterOption)
Fragment C ViewModel --> contains getZlist(selectedFilterOption)
So first filter options is fetched from the server and then adapter is set with the first selected option.
As its a viewpager it loads the first as well as second fragment i.e Fragment A and Fragment B will be loaded.
Now suppose, Fragment A is visible and fragment B is already loaded and filter option is changed. As different filter is selected the data needs to be updated in every fragment based on the selected filter.
This is handled like this :
As filter is changed , the current fragment's data is reloaded. OnPageChangeListener is implemented and on its change respective fragment's data is reloaded.
But due to this, everytime a tab is changed ,api call is triggered and data is fetched.
Second thing when the activity is loaded for first time it has already loaded the second fragment. So on tab change the second fragment's data is going to be loaded again which should not happen.This can be avoided with the checks but its going to be a mess.
Is there any better approach using observable pattern, the fragments data can be loaded after filter option is changed.
You can call api once the activity is started..After getting the data
you can create tab and through the viewpager you can pass the value
with doing filteration.. This will not call api again as long as you
stay in activity
Additionally if you want some observable you can see and use greenrobot event bus. It's pretty simple eventbuslibrary

Send data from a fragment to another fragment with nested viewpager

how to send data from a fragment that is part of a viewpager, to another fragment that is also in a viewpager. The problem is when in fragment 1 by a spinner select a data, this data has to go to fragment B.
I solved it using RxBus2. Use the Warrenth library. I leave the link to your repoitorio https://github.com/warrenth/RxBus2

Send Data Between two Tab - android

I have create Tabbed activity with two tab
fragment a Contain edittext and fragment b Contain listview ,and I want to send data from fragment a to fragment b ,Please add the code in full,
please help me.
Note : I do not want pass data to textview Iwant to listview
All Fragment-to-Fragment communication is done through the associated Activity. Two Fragments should never communicate directly.
Follow this link

Switching between 2 fragments in the same activity without refreshing the data source

I have Fragment1 and Fragment2, RecyclerView1 corresponding to Fragment1, RecyclerView2 corresponding to Fragment2.
Both RecyclerViews have the same data source (the same information).
I use a button, let's say SwitchButton, to switch between the 2 fragments. Both fragments are in the same activity.
Use case: scrolling in RecyclerView1 to position 10, then clicking on the SwitchButton will show Fragment2 with the RecyclerView2 scrolled to position 10.
Where to keep the data? Because I don't want to make calls to server each time I switch between Fragment1 and Fragment2. Hope you understand. Thanks!
You can do it with the help of Interface.
1.create an interface with two methods one to set network call data and other to get the assigned network call data in activity.
2.In both fragments before making network call check whether the assigned data in activity using interface exist.
if it doesn't exist , make network call, save the data to activity variable.
when the fragment destroyed and recreated. the data will not be lost.

Load data into ListFragments

I would like to load data from network into fragments. Inside the Activity I have a ViewPager and I am using a FragmentPagerAdapter to provide fragments to it. The problem is:
How can I detect from activity, that ALL fragments from ViewPager are created and ready to show data. How can I inform fragments, that there is a data inside activity, that should be shown?
How can I detect from activity, that ALL fragments from ViewPager are
created and ready to show data.
This sounds a bit strange to ask. You Activity doesn't need to know when all of the ViewPager fragments are initialized(as only one will actually be seen by the user(plus one on each side will be available if you don't mess with setOffScreenPageLimit()), it doesn't make sense to update the rest).
How can I inform fragments, that there is a data inside activity, that
should be shown?
You fragments could register themselves as listeners for an Activity data load event. The Activity will do it's job of getting the data and then call the update method on all of the registered fragments(which should be stored in WeakReferences to avoid holding on to them when we shouldn't). The main problem is you'll risk trying to update items
Another hacky approach is to use something like from the Activity:
// get the current fragment(add/subtract one for the available fragments on each side of the curently visible
Fragment f = getSupportFragmentManager().findFragmentByTag("android:switcher:" + R.id.ViewPagerId + ":" + mViewPager.getCurrentItem()); one.)
f.update();
The rest of the fragments will check for new data being available when they get recreated.

Categories

Resources