Created
May 16, 2018 17:24
-
-
Save anitaa1990/7d52effe43c90031bcd8b4906158ebba to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class TimerTaskReferenceLeakActivity extends Activity { | |
private CountDownTimer countDownTimer; | |
@Override | |
protected void onCreate(@Nullable Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_first); | |
startTimer(); | |
} | |
public void cancelTimer() { | |
if(countDownTimer != null) countDownTimer.cancel(); | |
} | |
private void startTimer() { | |
countDownTimer = new CountDownTimer(1000, 1000) { | |
@Override | |
public void onTick(long millisUntilFinished) { | |
final int secondsRemaining = (int) (millisUntilFinished / 1000); | |
//update UI | |
} | |
@Override | |
public void onFinish() { | |
//handle onFinish | |
} | |
}; | |
countDownTimer.start(); | |
} | |
/* | |
* Fix 1: Cancel Timer when | |
* activity might be completed | |
* */ | |
@Override | |
protected void onDestroy() { | |
super.onDestroy(); | |
cancelTimer(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment