1

I am creating an Android app, and I have linked aFragmentStatePagerAdapter to a TabBar inside one of my Activities to allow users to slide from Fragment to Fragment. Each Fragment in this Activity is populated by a REST call, and if it fails, a Dialog will pop-up saying you should try and refresh your information (only the first time you view the Fragment). The problem I am having is that since FragmentStatePagerAdapter creates the neighboring Fragments to the Fragment you are currently on, it is creating the Dialog(s) of said Fragments prematurely (For example if you are on Fragment 3, and Fragment 4 prompts a Dialog, it will show on Fragment 3 instead of Fragment 4). Is there a way to turn off the creation of neighboring Fragments using a FragmentStatePagerAdapter?

Thank You!

2 Answers2

0

You can control the number of neighboring fragments that area created by calling setOffscreenPageLimit, but the minimum is 1.

Instead of creating your Dialogs in the fragments' onCreateView, I would do it setUserVisibleHint instead. That way the dialog is only created when you're certain that the user is on the page.

Cigogne Eveillée
  • 2,178
  • 22
  • 36
0

No i think you can't , Though there is a property for view pager

  viewpager.setOffscreenLimit(0) //defaults to 1

but you can try some other solutions

  1. show Dialog in onResume();
  2. make an interface in Acitvity and fire up to it's implementation in Fragment

    tab.setOntabselected{

    Pageselected(pagenumber)

}

Nandhu
  • 158
  • 9