Niko Adrianus Yuwono
Niko Adrianus Yuwono

Reputation: 11112

ViewPager with Fragment reload at first and last page

I'm trying to create a ViewPager with six fragments but only 2nd fragment to 5th fragment contain data that I want to show and the first fragment and the last fragment I want to be used to reload the data and set the position to the 2nd fragment again. The overall flow is like this :

1st (reload and go back to 2nd) <- 2nd fragment <-> 5th fragment -> 6th fragment (same with 1st)

what I've tried is I create a callback from the 1st fragment and 6th fragment like this

public static class callbackFragmentLoading implements callbackFragmentLoad {

    @Override
    public void onLoading() {
        mPager.setAdapter(mAdapter);
        mPager.setCurrentItem(2,false);
    }

}

and I passed the callback to the fragment constructor so I can called the onLoading function in the onActivityCreated. But I everytime I do it the application will be force closed and the logcat shows

recursive entry to executependingtransactions

is there any way to do this? or my method for doing it is wrong?

Thank You

Upvotes: 2

Views: 2948

Answers (1)

user
user

Reputation: 87064

is there any way to do this? or my method for doing it is wrong?

Messing with callbacks between Fragments of a ViewPager isn't probably such a good idea. Instead I would do it like this:

  • Don't load any data(like with a Loader) in the Fragments from the ViewPager, instead let the FragmentActivity do it(and the Fragments will get it through methods from the Activity).
  • Your two loading fragments(position 0 and 5) will call in their onResume method a reload action on the parent Activity(like a Loader restart)
  • At this moment the Activity will load/reload the data and when that finishes it will set the ViewPager to the correct items(either 1 or 4)
  • in the onResume method of the data fragments you'll refresh the fragment's data(here you may need to use some sort of signaling system because you'll need to duplicate the refresh code in the onCreateView(some fragments may have their view destroyed if they are far apart from the current visible position)).

As I don't know many things about the inner data fragment I've written a basic skeleton sample(without the data loading in the activity).

Upvotes: 2

Related Questions