LinearTimer
LinearTimer copied to clipboard
Make countUpTimer`handler` static to prevent memory leak.
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);
}
}
}
};