Created
May 16, 2018 17:21
-
-
Save anitaa1990/86e881e0eb1b24cfd959723203979d48 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(); | |
} | |
/* | |
* Mistake 1: Cancel Timer is never called | |
* even though activity might be completed | |
* */ | |
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(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment