I have a ViewPager with 10 pages. When I start the last (10th) page onCreateView()
method of my fragment is called. When I swipe to the 9th page onCreateView()
is called also. But when I back to the 10th page onCreateView()
isn't called. What's wrong?

- 1,399
- 6
- 21
- 40
-
Post the code for we understanding what is going on – Paulo Aug 29 '14 at 18:36
-
Your ViewPager adapter may cause this problem. But i cannot know without seeing your code... – Oğuzhan Döngül Aug 29 '14 at 18:37
-
Any proper solution for this issue? – Jack Feb 02 '18 at 05:52
6 Answers
Try Extending FragmentStatePagerAdapter

- 1,284
- 14
- 24
-
It should be accepted Answer, it works Perfect for me. Thanks. :) – Naeem Ibrahim Apr 11 '18 at 11:04
-
-
@MehrdadSComputer The fragment is recreated when page is selected. so onCreateView is called – Vinay Oct 29 '18 at 10:51
-
@Vinay when we extend `FragmentPagerAdapter`, the same won't happen? – Mehrdad Salimi Oct 29 '18 at 14:09
-
@MehrdadSComputer Yes. the fragment is saved when FragmentPagerAdapter is used . when the user returns to previous fragment it is displayed taking from memory instead of re-creating the fragment. – Vinay Oct 29 '18 at 16:19
That is because a FragmentPagerAdapter keeps in memory every fragment. Hence, when you visit the first time the fragment, onCreate will be invoked but the second time Android will looking for in memory, so it not need invoke onCreate.
If you need run the code in OnCreate every time fragment is displayed, you should move it to getItem(int id)
See offical documentation: http://developer.android.com/reference/android/support/v4/app/FragmentPagerAdapter.html

- 3,016
- 6
- 38
- 65
-
Thank you, but I added Log.i("hello", "hello" + id); to getItem method, and this log I only see when I start the viewpager. When I swipe between pages it doesn't invoke. – Bakus123 Aug 29 '14 at 19:06
-
-
-
onResume for Fragment, or if you need call that code in OnCreate and in OnResume, you can write a separate function and invoke it from that two methods. – Fran b Aug 29 '14 at 19:22
Nothing is wrong. The ViewPager
already has the page, and so it does not need to create it.

- 986,068
- 189
- 2,389
- 2,491
-
3How can I do it when any of method (onStart, OnCreate, onCreateView) isn't inoke? – Bakus123 Aug 29 '14 at 19:19
-
@Bakus123: One possibility is to add an `OnPageChangeListener` to the `ViewPager`, so you know when it changes pages. Or, just update the page when the data changes, rather than waiting for the user to swipe to it, for all pages that have been created so far. – CommonsWare Aug 29 '14 at 19:21
-
@Bakus123: You will need to keep track of which fragments exist and belong to which positions. Then, you can call some method on your fragments to tell them to update based upon the page change. – CommonsWare Aug 29 '14 at 19:39
I had the same problem, my solution was to assign again the adapter of the ViewPager instance, just like:
pager.setAdapter(adapter);
This causes a restart of the "mItems" property from the viewPager and removes the cache.
But I don't know if it's a safe solution

- 249
- 1
- 3
- 11
You can call the adapter getItem from onPageSelect, which is called also on swipes, and place your code inside the getItem, or even in the onPageSeelect itself.

- 2,354
- 2
- 26
- 37
CommonWare's answer is the best and works like charm:
simple add OnPageChangeListener
to your ViewPager
item, something like this:
ViewPager viewPager = null;
PagerAdapter pagerAdapter = null;
//Some code come here...
pagerAdapter = new PagerAdapter(); //Or any class derived from it
viewPager = (ViewPager)findViewById(R.id.container);//Connect it to XML
viewPager.setAdapter (mPagerAdapter); //Connect the two
//Next two lines are simply for fun...
//viewager.setPageTransformer(true, new DepthPageTransformer());
//viewPager.setPageTransformer(true, new PaymentZoomOutPageTransformer());
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
//This is the right place to connect the pages with a data struct!!!
@Override
public void onPageSelected(int position) {
// Here you can connect the current displayed page with some data..
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
//Here use the inflater to add views/pages
//Don't forget to do:
pagerAdapter.notifyDataSetChanged();
//When you're done...

- 356
- 4
- 8