Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8810967
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T03:14:51+00:00 2026-06-14T03:14:51+00:00

All I want is to create new separate timer for each page in the

  • 0

All I want is to create new separate timer for each page in the ViewPager.
My current realization contains the one handler for all of the pages. I start new TimerTask using ViewPager.OnPageChangeListener.onPageSelected(), but as I can see during my application working, runnables stacks and timer goes faster and faster, updating all textviews at one time, even though I’m trying to remove all callbacks in the handler.

How it should work: swipe to page 1 — timer 1 starts, swipe to page 2 — timer 1 resets and stops, timer 2 starts and so on.

viewPager.setOnPageChangeListener(new OnPageChangeListener() {

        public void onPageScrollStateChanged(int page) {

        }

        public void onPageScrolled(int position, float arg1, int arg2) {
        }

        public void onPageSelected(int position) {
            View currentPage = ((QuestionPagerAdapter) viewPager.getAdapter()).get(position);

            timerText = (TextView) currentPage.findViewById(R.id.timerText);
            handler.removeCallbacks(doTick);
            if (timerText != null) startTimer();
        }

    });

Runnable realization:

final Runnable doTick = new Runnable() {

    public void run() {
        seconds--;
        if (seconds == -1) {
            minutes--;
            seconds = 59;
        }
        String secText = String.valueOf(seconds);
        if (seconds < 10) secText = "0" + secText;
        timerText.setText("0" + String.valueOf(minutes) + ":" + secText);
        viewPager.getAdapter().notifyDataSetChanged();
    }

};

private void startTimer() {
    for (int i = 1; i <= seconds; i++) {
        TimerTask task = new TimerTask() {
            @Override
            public void run() {
                handler.post(doTick);
            }
        };
        timer.schedule(task, i * 1000); 
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T03:14:52+00:00Added an answer on June 14, 2026 at 3:14 am

    Where do you instantiate new doTick Runnable? This should be done only once, otherwise, you will not be able to remove all callbacks associated to a doTick instance as you would have lost it and replaced it with a new instance.

    Declare it has a data field and modify you startTimer method as follow :

    private void startTimer( TextView textView ) {
        if( doTicks != null  ) {
           handler.removeCallbacks(doTick);
           doTicks = new doTicks( textView );
        }
        for (int i = 1; i <= seconds; i++) {
            TimerTask task = new TimerTask() {
                @Override
                    public void run() {
                        handler.post(doTick);
                    }
                };
            timer.schedule(task, i * 1000); 
        }
    }
    

    You will also need to provide a parameter to your doTicks Runnable to give it a reference to the field it has to update. Pass this parameter to startTimer as well, and don’t keep a reference on this field as the data member of the activity, only as a data member of doTicks (so you need to create a true static inner class).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to create one new model class, which will be extended in all
I want to create a string in JavaScript that contains all ascii characters. How
I want to create the dictionary of all the ViewModels. public static Dictionary<string, WeakReference>
I want to create a wrapper class so that all queries should not be
I want to create a logger which logs all my messages sent to System.out
i want to create a simple state machine using C but all the events
I want to create JSF table which has select all button and check box
I want to create an Android app that can be used on 2.3.3 all
I want to create and then destroy buttons, all with code, but can't find
I want to create a Pong game with a moving ball and all the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.