1

I have one fragment where are three (default) images and when user click on them, they will change to another. But when i swipe to another fragment and back to fragment with images there are not default ones as on the start. When I swipe two times so I will pass to another fragment (distance from original with images is 2 fragments) images are resetted to default. I was trying to implement setOffscreenPageLimit() from ViewPager and set it to 1, but minimum "length" when views in fragments are resetted is 2. How can I change that images to default manually after swipe action? Thank you.

Edit: I think that issue why onResume() is not working here: Fragment onResume not called

but i dont know what that means :/ I have three classes FragmentController.class, PagerAdapter.class and class of specific fragment for example FirstFragment.class. I don't know how connect these classes together.

idature
  • 491
  • 7
  • 23
  • reset the view on `onResume` method – Agi Maulana Aug 31 '17 at 16:35
  • ```onResume``` is not working properly I print when it is triggered but It will be only triggered when i swipe from first fragment to second (third is the fragment with images which I want to reset) of course i have onResume() method in third fragment class. – idature Aug 31 '17 at 16:54

1 Answers1

0

Check that you create the fragments in the getItem() method of the adapter and do not hold any reference to that fragments (only WeakReferences if necessary), otherwise the fragments could not be destroyed.

EDIT:

The first fragment is unloaded only when you are in the third one because setOffscreenPageLimit is at least 1 so a viewpager allways loads the fragments that are at both sides of the selected one.

What you could do is to update your adapter with this code to provide a getFragment(position) method:

private HashMap<Integer, WeakReference<Fragment>> fragmentReferences = new HashMap<>();

@Override
public Fragment getItem(int position) {
    Fragment fragment;
    switch (position) {
        case 0:
            fragment = FirstFragment.newInstance();
            break;
        // etc
    }
    fragmentReferences.put(position, new WeakReference<>(fragment));
    return fragment;
}

public Fragment getFragment(int position) {
    WeakReference<Fragment> ref = fragmentReferences.get(position);
    return ref == null ? null : ref.get();
}

After then you can get the selected fragment and call the method you want from the first fragment when a page is selected:

viewPager.setOnPageChangeListener(new OnPageChangeListener() {
    @Override
    public void onPageSelected(int currentPage) {
        if (currentPage == 0) {
            Fragment firstFragment = adapter.getFragment(0);
            if (firstFragment != null) {
                // This method resets the images of the first fragment
                ((FirstFragment) firstFragment).reset();
            }
        }
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
        // Do nothing
    }

    @Override
    public void onPageScrollStateChanged(int state) {
        // Do nothing
    }
});
jeprubio
  • 17,312
  • 5
  • 45
  • 56
  • getItem switches position and on the actual position it will returns FirstFragment.newInstance(); (FirstFragment is class of actual fragment) – idature Aug 31 '17 at 17:02
  • Add some logs or a breakpoint at the beginning of the getItem method and swipe from the third fragment to the second one. The getItem() is being called for the first position and returning this new instance? – jeprubio Aug 31 '17 at 17:44
  • I added print before return of newInstance in getItem and every case (fragment) is printed once when is created when i was swiping again there is nothing printed again so you can swipe how much you want but every newInstance is called only once. – idature Aug 31 '17 at 20:25
  • And what about the getItem(position)? Is it called with the position 0 as parameter when you swipe from the 3rd to the 2nd fragment? – jeprubio Aug 31 '17 at 20:37
  • It prints position good but only once its the same as in the debug before. – idature Aug 31 '17 at 20:41
  • Sorry, I read all the description again and now I think I understand what you need, I edit my answer. – jeprubio Aug 31 '17 at 21:14
  • Can you tell me where exactly i must put that setOnPageListener? To my fragment class or pageAdapter or fragmentController? And to which method? – idature Aug 31 '17 at 21:47
  • In your fragment class, where there is the ViewPager object. You can replace the code you had of viewPager.setOffscreenPageLimit(1) with this one. – jeprubio Aug 31 '17 at 21:56
  • I done everything you said, but .reset() It cannot resolve that method. Do you know why? – idature Aug 31 '17 at 22:03
  • Yes, I added the comment on the top saying that the reset method is the place where you have to write the code to reset the images. You can choose the name you like for this method but, to use this code, a public method must be written in the FirstFragment class to reset the state of the images. This method will be called every time this first fragment is selected. – jeprubio Aug 31 '17 at 22:09