LinearTimer icon indicating copy to clipboard operation
LinearTimer copied to clipboard

Make countUpTimer`handler` static to prevent memory leak.

Open krtkush opened this issue 7 years ago • 0 comments

https://www.androiddesignpatterns.com/2013/01/inner-class-handler-memory-leak.html

Refer - countUpTimer.java: Line 87

private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            synchronized (CountUpTimer.this) {
                long elapsedTime;

                elapsedTime = SystemClock.elapsedRealtime() - base;

                // If elapsed paused time is not zero, reset them to zero so as to begin tracking
                // any further instances of pause button being tapped on
                if(elapsedPausedTime != 0L){
                    elapsedPausedTime = 0L;
                    pauseStart = 0L;
                    pauseEnd = 0L;
                }

                // If condition set up to hinder onTick callBacks being sent if
                // elapsedTime somehow is more than the duration.
                // Stop the timer if it has run for the required duration.
                if(elapsedTime >= duration) {
                    stop();
                } else {
                    onTick(elapsedTime);
                    sendMessageDelayed(obtainMessage(MSG), interval);
                }
            }
        }
    };

krtkush avatar Jan 11 '18 07:01 krtkush