2

Lets assume i have three fragments inside viewPager adapter.

Does it create view (on create view called) every time i scroll to that fragment

OR

is it called only in initialization in main activity when we try to set adapter?

If it is the second, then heavy work will be done at the beginning. If it is first, then i have to save fragments states and must not call its init methods all the time.

Thanks in advance.

    List<Fragment> l = new ArrayList<>();
    l.add(Fragment.instantiate(this, DashBoard.class.getName()));
    l.add(Fragment.instantiate(this,Expenses.class.getName()));
    l.add(Fragment.instantiate(this,Gross.class.getName()));
    SliderAdapter sa = new SliderAdapter(getSupportFragmentManager(),l);
    vp = (ViewPager) findViewById(R.id.viewPager);
    vp.setAdapter(sa);
K Guru
  • 1,292
  • 2
  • 17
  • 36
Mert Serimer
  • 1,217
  • 2
  • 16
  • 38

2 Answers2

1

Yes the onCreteView is called everytime you swipe th fragments

Solution : One of the solution can be using :

public void setOffscreenPageLimit (int limit) api

Set the number of pages that should be retained to either side of the current page in the view hierarchy in an idle state. Pages beyond this limit will be recreated from the adapter when needed. refer Android Doc

KOTIOS
  • 11,177
  • 3
  • 39
  • 66
  • Does the either side includes this conditions such as "end being side of the beginning"? so pos 0 is side of last? I think probably not – Mert Serimer Feb 05 '15 at 08:37
  • Also what is that "Pages beyond this limit will be recreated from the adapter when needed. " mean? Does it destroy fragments which are beyond from the offscreen limit? – Mert Serimer Feb 05 '15 at 08:41
  • int limit says that from default position 0 to last position it will cache it ..let me nkwo if any issues – KOTIOS Feb 05 '15 at 09:06
1

No , its not create every time you swipe left or right .it keeps additional pages .but you can create it every time by setting off screen page limits .

public void setOffscreenPageLimit (int limit)

for Reference please check this

Community
  • 1
  • 1
K Guru
  • 1,292
  • 2
  • 17
  • 36