Performance for Android ViewPager and FragmentPagerAdapter with 3 Web Related Fragments

838 Views Asked by At

This is my first question. So please forgive me if my question is dummy quest.

I search some questions and answer link and I think there is no 100% match for me.

I'm trying to create an android application. It contain 4 fragment in one activity with ViewPager. And using FragmentPagerAdapter for swipe view.

Three of these fragment need to fetch and sync data from web.

FragmentPagerAdapter is pre load for next swipe(next fragment) when my Activity start. (It's my problem). The first fragment is WebView. And second fragment need to fetch a lot of data from web service or local DB (if there is no data in local DB, need to fetch from web) then show with ListView and third is also. Fourth need to fetch little data.

When Main Activity start, two fragment load data from web. First one fetch to WebView and second one fetch to AsyncTask. So there is too long to complete this tasks. (My internet speed is around 100Kbps). The second fragment need to do two thing; first one is fetch from web and save to local DB, second one is load data from local DB to show user with list. **I think this is bad performance for user.

I want to load or sync data when user swipe on each fragment. Currently I used ViewerPager> onPageSelected method to update fragment. But it create new Instance and gave me error. How to update local DB and show ListView when user select on?

Any solution for it?

I did not finish overall codes. I tested with some virtual data for second fragment make me slow. And I dare to go on my project. I guess do I need to redesign! I've no idea.

1

There are 1 best solutions below

1
On

First, use FragmentStatePagerAdapter. Then, Don't load or refresh second fragment (that's the one with list view loading local or online DB) on onCreate or onResume. Use otto (It's an event bus system) and do like this. in your main activity view pager onPageChangeListener

public void onPageSelected (int position){
  bus.post(new LoadingEvent());} //

and in your Second Fragment

@Subscribe public void loadData(LoadingEvent event) {
// TODO: Load Data from local DB or online

}

Do like that on third fragment or fourth if you need Checkout the otto page for more usage.