-1

In my Log current position is called twice for the same value and my timer takes almost twice time. NUM_PAGES is actually taking the maximum size of my array list.

    NUM_PAGES = detailsArrayList.size();

    // Auto start of viewpager
    final Handler handler = new Handler();
    final Runnable Update = new Runnable() {
        public void run() {
            if (currentPage == NUM_PAGES) {
                currentPage = 0;
            }
            Log.d(TAG, "Current Page: " + currentPage);
            mPager.setCurrentItem(currentPage++, false);
        }
    };
    final Timer swipeTimer = new Timer();
    swipeTimer.schedule(new TimerTask() {
        @Override
        public void run() {
            handler.post(Update);
        }
    }, 5000, 5000);

    // Pager listener over indicator
    indicator.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

        @Override
        public void onPageSelected(int position) {
            currentPage = position;
        }

        @Override
        public void onPageScrolled(int pos, float arg1, int arg2) {
        }

        @Override
        public void onPageScrollStateChanged(int pos) {
        }
    });
Dan Cantir
  • 2,915
  • 14
  • 24
Rahul
  • 322
  • 1
  • 5
  • 18

1 Answers1

1

I've tried a similar, simplified code snippet shown below:

public class MainActivity extends ActionBarActivity { int counter = 0;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    timer.schedule(new TimerTask() {
        @Override
        public void run() {
            handler.post(Update);
        }
    }, 500, 500);
}

// Auto start of viewpager
final Timer timer = new Timer();
final Handler handler = new Handler();
final Runnable Update = new Runnable() {
    public void run() {
        Log.d("TimerTest", "First Log: " + counter);
        try {
            Thread.sleep(50);
        } catch (InterruptedException e) {
            Log.d("TimerTest", e.toString());
        }
        //Log.d("TimerTest", "Second Log: " + counter);
        setCounter(counter++);
    }
};

void setCounter(int c) { counter = c; }

The result of the code is:

03-01 19:03:25.936 11865-11865/il.co.falk.timertest D/TimerTest: First Log: 0
03-01 19:03:26.436 11865-11865/il.co.falk.timertest D/TimerTest: First Log: 0
03-01 19:03:26.946 11865-11865/il.co.falk.timertest D/TimerTest: First Log: 0
03-01 19:03:27.456 11865-11865/il.co.falk.timertest D/TimerTest: First Log: 0
03-01 19:03:27.966 11865-11865/il.co.falk.timertest D/TimerTest: First Log: 0
03-01 19:03:28.476 11865-11865/il.co.falk.timertest D/TimerTest: First Log: 0

The reason for this is that the increment operator (++) is postfix. It returns the old value and then it is incremented.

From here on I can only speculate. You don't have a set function but you have something similar with onPageSelected. It is invoked when the page becomes selected. I'm guessing you have a race condition. If it happens after the increment, you'll stay in place like my example. Otherwise, the page will move by one.

Roy Falk
  • 1,685
  • 3
  • 19
  • 45
  • I tried this but not working. Actually what happening is when we came from some other page to this page then its simply decrease the time by half. if we came from other pages using finish() method then it does the right thing. – Rahul Mar 02 '16 at 06:21
  • You should probably add the question to include all the relevant code. – Roy Falk Mar 02 '16 at 06:50