Tarek Zoabi
Tarek Zoabi

Reputation: 23

My stopwatch resets to zero after pause button is pressed, what could be wrong in my code?

I created a stopwatch application, and when I tested it I noticed a problem which is that the stop watch resets to zero after pause button is pressed. (when the user presses start after he paused the button the stopwatch starts from zero). Here's my code:

TextView tv;
EditText laps_editText;
Button btnstart, btnreset;
int a, b, c, d, e, f;
String splString, diffString;
long split, diff, firstsplit = 0;
int t = 1;
long startTime = 0;
long timeInMilis = 0;
long timeSwap = 0;
long updatedtime = 0;
int sec = 0, min = 0, hr = 0;
int secs = 0, mins = 0, hrs = 0;
int secspl = 0, minspl = 0, hrspl =0;
Handler handler = new Handler();
boolean isRunning = false;
String spl, dif;
int counter = 1;
ScrollView scrollView;
Button changeColor;
int selectedColor, savedColor;
SharedPreferences backGroundColor = null;
Context context;
RelativeLayout rLayout;
int n = 0;
SharedPreferences current = null;
long oldReciver;

 @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View v =  inflater.inflate(R.layout.fragment_stop_watch, container, false);
        laps_editText = (EditText) v.findViewById(R.id.lap_edittext);

        rLayout = (RelativeLayout) v.findViewById(R.id.r_layout);
        context = getActivity().getApplicationContext();
        tv = (TextView) v.findViewById(R.id.textView2);



        btnstart = (Button) v.findViewById(R.id.button3);
        btnreset = (Button) v.findViewById(R.id.button4);

        btnstart.setTypeface(custom_font);
        btnreset.setTypeface(custom_font);
        changeColor = (Button) v.findViewById(R.id.changeColor);

        // As you see I tried to use sharedpreferences to save the updatedtime variable that was before the user pressed the pause button
        current = context.getSharedPreferences("currentMil", Context.MODE_PRIVATE);

        savedColor = backGroundColor.getInt("color", 38536);
        rLayout.setBackgroundColor(savedColor);



        btnstart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
// if the t == 1 the button will start the stopwath else it will stop it
                    if(t == 1){
                            oldReciver = current.getLong("updated", 00);
                            isRunning = true;
                            btnstart.setText("Pause");
                            btnreset.setText("Lap");
                            startTime = SystemClock.uptimeMillis() + oldReciver;
                            t = 0;
                            handler.postDelayed(updateTimer, 0);


                }
                else{
                    isRunning = false;
                    btnstart.setText("Start");
                    btnreset.setText("Reset");
                    timeSwap += timeInMilis;
                    handler.removeCallbacks(updateTimer);
                    n = 1;
                    t = 1;
                }
            }
        });
        btnreset.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
              /*  if(isRunning){
                    split = SystemClock.uptimeMillis() - startTime;
                    secspl = (int)(split / 1000);
                    minspl = sec / 60;
                    hrspl = min / 60;
                    secspl = sec % 60;

                    if (firstsplit == 0){
                        diff = split;
                        secs = (int)(diff / 1000);
                        mins = sec / 60;
                        hrs = min / 60;
                        secs = sec % 60;
                        firstsplit = split;
                    }
                    else{
                        diff = split - firstsplit;
                        secs = (int)(diff / 1000);
                        mins = sec / 60;
                        hrs= min / 60;
                        secs = sec % 60;
                        firstsplit = split;
                    }
                    dif = String.valueOf(hrs) + ":" + String.valueOf(mins) + ":" + String.valueOf(secs);
                    spl = String.valueOf(hrspl) + ":" + String.valueOf(minspl) + ":" + String.valueOf(secspl);




                }
ignore those lines above they are just for laps 
*/
                    else{
                        startTime = 0;
                        t = 1;
                        timeInMilis = 0;
                        timeSwap = 0;
                        sec = 0;
                        min = 0;
                        hr = 0;
                        updatedtime = 0;
                        laps_editText.setText("");
                        btnstart.setText("Start");
                        handler.removeCallbacks(updateTimer);
                        tv.setText("00:00:00");
                        n = 1;
                        counter = 1;
                    }
                }
                public String getDiff(){
                    return dif;
                }
                public String getSpl(){
                    return spl;
                }

            });


            return v;

        }

 public Runnable updateTimer = new Runnable() {
        @Override
        public void run() {
            timeInMilis = SystemClock.uptimeMillis() - startTime;
            updatedtime = timeSwap + timeInMilis;
// here i,m tring to save updatetime value
            SharedPreferences.Editor editor = current.edit();
            editor.putLong("updated", updatedtime);
            editor.commit();
            sec = (int)(updatedtime / 1000);
            min = sec / 60;
            hr = min / 60;
            sec = sec % 60;
            tv.setText(String.format("%02d", hr) + ":" + String.format("%02d", min) + ":" + String.format("%02d", sec)  );
            handler.postDelayed(this, 0);
        }
    };

Any help?!

EDIT 1: I just added a long variable "until pause" which equals zero and calcaulated the difference in time from the moment ther user presses the button start and the pause button and it solved the issue.

Upvotes: 0

Views: 49

Answers (2)

Uchiha Itachi
Uchiha Itachi

Reputation: 1271

You don't have a Pause button. You just change the text of your Start button to "Pause". So, every time you click "Pause", you're really just clicking Start again. If you want to actually "Pause" it, you either need to put in a real Pause button, or run a check to see the content of the button when it's clicked:

long currTime = 0;
if (btnStart.Text == "Start")
{
    if (timeStart == 0;)
        timer.Start();
    else if (timeStart > 0)
        timeStart = currTime;
}
else if (btnStart.Text == "Pause")
{
    timer.Pause();
    currTime = timeStart;
}

I can't see you're code from here, but you should have a Start() that increments startTime and your Reset() should set startTime back to 0.

This isn't precise. Just a guide on how you should approach it. It's doesn't need to be insanely complicated - in fact, if it is, you're probably doing it wrong. Simpler is better!

Upvotes: 0

Ivan Georgiev
Ivan Georgiev

Reputation: 1185

I cannot see where the oldReceiver variable is defined and what is its type. Maybe some casting happens which results in zero.

Good practice would be to define the property name as "constant".

It might be also better to check the button label to decide if it is pause or start click.

Upvotes: 1

Related Questions