0

I have a stop watch, it work right, but when i press home button or button closing bell and reopen it after some time, I see wrong time in my stop watch. Tell me please what I can do to solve this problem ?

public class Stopwatch extends Activity {
    Timer mTimer;
    int second_f_p = 0, second_s_p = 0, millisecond = 0;
    int minute_f_p = 0, minute_s_p;
    int hour_f_p = 0, hour_s_p = 0;
    int numberOfLap = 0;
    TextView tv;
    TextView TVlap;
    Button btn_start;

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

        btn_start = (Button) this.findViewById(R.id.button1);
        tv = (TextView) this.findViewById(R.id.tvTime);
        btn_start.setOnClickListener(new View.OnClickListener() {@Override
            public void onClick(View arg0) {
                btn_start.setClickable(false);
                mTimer = new Timer();
                mTimer.schedule(new TimerTask() {

                    @Override
                    public void run() {
                        timerMethod();
                    }

                }, 0, 100);

            }
        });

        Button btn_stop = (Button) this.findViewById(R.id.button2);
        btn_stop.setOnClickListener(new View.OnClickListener() {@Override
            public void onClick(View v) {
                btn_start.setClickable(true);
                mTimer.cancel();
            }

        });

        Button btn_reset = (Button) this.findViewById(R.id.button4);
        btn_reset.setOnClickListener(new View.OnClickListener() {@Override
            public void onClick(View v) {

                second_f_p = 0;
                second_s_p = 0;
                minute_f_p = 0;
                minute_s_p = 0;
                millisecond = 0;
                hour_f_p = 0;
                hour_s_p = 0;
                numberOfLap = 0;
                TVlap.setText("");
                tv.setText(String.valueOf(hour_f_p) + String.valueOf(hour_s_p) + ":" + String.valueOf(minute_f_p) + String.valueOf(minute_s_p) + ":" + String.valueOf(second_f_p) + String.valueOf(second_s_p) + "." + String.valueOf(millisecond));
            }

        });
        TVlap = (TextView) this.findViewById(R.id.tvLap);
        Button btn_lap = (Button) this.findViewById(R.id.button3);
        btn_lap.setOnClickListener(new View.OnClickListener() {@Override
            public void onClick(View v) {
                numberOfLap++;
                TVlap.setText(TVlap.getText() + String.valueOf(numberOfLap) + ". " + String.valueOf(hour_f_p) + String.valueOf(hour_s_p) + ":" + String.valueOf(minute_f_p) + String.valueOf(minute_s_p) + ":" + String.valueOf(second_f_p) + String.valueOf(second_s_p) + "." + String.valueOf(millisecond) + "\n");
            }

        });

    }

    private void timerMethod() {
        this.runOnUiThread(doSomething);
    }

    private final Runnable doSomething = new Runnable() {@Override
        public void run() {
            millisecond++;

            if (millisecond == 10) {
                second_s_p++;
                millisecond = 0;

            }

            if (second_s_p == 10) {
                second_f_p++;
                second_s_p = 0;
            }
            if (second_f_p == 6) {
                minute_s_p++;
                second_f_p = 0;
            }
            if (minute_s_p == 10) {
                minute_f_p++;
                minute_s_p = 0;
            }
            if (minute_f_p == 6) {
                hour_s_p++;
                minute_f_p = 0;
            }

            if (hour_s_p == 10) {
                hour_f_p++;
                hour_s_p = 0;
            }

            if (hour_f_p == 9 && hour_s_p == 9) {
                second_f_p = 0;
                second_s_p = 0;
                minute_f_p = 0;
                minute_s_p = 0;
                hour_f_p = 0;
                hour_s_p = 0;
            }
            tv.setText(String.valueOf(hour_f_p) + String.valueOf(hour_s_p) + ":" + String.valueOf(minute_f_p) + String.valueOf(minute_s_p) + ":" + String.valueOf(second_f_p) + String.valueOf(second_s_p) + "." + String.valueOf(millisecond));
        }

    };

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_stopwatch, menu);
        return true;
    }

}
Vipul
  • 27,808
  • 7
  • 60
  • 75
Kostya Khuta
  • 673
  • 2
  • 7
  • 21
  • I think you have to add code in order for any of us to check what you're missing? – Rob Nov 26 '12 at 07:27
  • I think it's probably related to the Activity lifecycle. You may want to implement a service to make it long living. Anyway, the information you provide is too little to make your question useful. – tanyehzheng Nov 26 '12 at 07:31
  • But when i press "Home" or "button closing bell" my program doesn't switch off,it works in background, but works wrong – Kostya Khuta Nov 26 '12 at 07:37

0 Answers0