Reputation: 3203
I have a ViewPager
which have contains two different fragments.
We assume that they names are A and B fragments.
One of these fragments has a date validation process. When a user attempts to navigate to B from A, if the selected date is later than today, the ViewPager turns back to Fragment A (rejecting navigation to B).
This is provided with onPageScrolled like below.
@Override
public void onPageScrolled( int position, float positionOffset, int positionOffsetPixels ) {
if ( position == 0 ) {
if ( positionOffset > 0.5 && getLastCycleDay() > DateHelper.CreateToday( false ) ) {
mPager.setCurrentItem( 1 );
SnackbarHelper.newInstance().snackbarshow( IntroActivity.this, resources.getString( R.string.dont_allow_new_cycle ) );
}
}
My question is which onPageScrolled
works twice and when I want to run a method in that, this method also works twice.
How can I prevent this situation ?
Upvotes: 3
Views: 751
Reputation: 15685
Events will fire more than once if wired-up more than once. Put a breakpoint on the myPager.setOnPageChangeListener
(or addOnPageChangeListener method, depending on API version) method, which wires up the event handler for the pager. Check why it's being wired up twice (if it is).
Given what you've described above, it's possible that there is a referencing problem, and that the events are for different instances of fragment A.
Try placing a breakpoint on the first line of code in the onPageScrolled event handler, and then using the "Evaluate Code Fragment" window (Run menu -> Evaluate Expression), type and evaluate 'this'. Check whether the instance of the ViewPager in the first and second firings is the same object (by using the instance ID, circled below):
If the numbers are not the same, it means that there are two instances of the fragment that each has its own event being fired, and the way that the fragments are set up and torn down will need to be reviewed.
Without more code, it's difficult to say.
Upvotes: 3