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 898429
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T15:02:26+00:00 2026-05-15T15:02:26+00:00

My activity has a simple menuItem that start a counter… the counter is stored

  • 0

My activity has a simple menuItem that start a counter…
the counter is stored in a field of the activity
the problem is with the “back button”
after closing the activity (back button), the timer (as in my intention) continues,
and restarting the activity behave correctly by reshowing the timer…
but I lost the reference to the counter and so I cannot stop it
Any ideas?

following there is a part of the code of the function
boolean onOptionsItemSelected(MenuItem item)

switch (item.getItemId()) {
    case R.id.STARTSTOP:
        running = !running;
        if (!running) {
            item.setTitle("START");
            if (counter!=null) {
                counter.cancel();
                counter = null;
            }
        }
        else {
            item.setTitle("STOP");
            counter = new CountDownTimer(1000*60*20/*20min*/,1000){
                @Override
                public void onFinish() {
                    TextView tv = (TextView) findViewById(R.id.counter);
                    tv.setText("done!");
                }
                @Override
                public void onTick(long millisUntilFinished) {
                    TextView tv = (TextView) findViewById(R.id.counter);
                    tv.setText(millisUntilFinished);
                    Log.d("XXXDEBUGXXX",millisUntilFinished);
                }
            };
            counter.start();
    }

}

  • 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-05-15T15:02:27+00:00Added an answer on May 15, 2026 at 3:02 pm

    I’d suggest to use AlarmManager instead.
    Or you could use SharedPreferences to store intermediate value that’s read in onResume(..) to restart the timer and saved in onPause(..).

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

Sidebar

Related Questions

I have an application that contains a main Activity, which has a very simple
I have a simple application that has 3 activities. The third activity is a
I created very simple activity that displays hello world and a button labled TEST,
I have a retain/release project that has a simple algorithm. I start out w/
I am trying to make a simple Activity which has a menu with two
Assume that we have 3 different activities. Each activity has its own XML file
I have a activity which has a button and 2 autocomplete widget. for the
Suppose that the Activity Tree has two activities (Activity 1 and Activity 2). Activity
Guys I have an activity which has a home button, when the button is
Here is the short version. I have a Bonus Activity that has 25 image

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.