Fix CountDownTimer handler memory leak
In the original implementation of the class CountDownTimer, the handler was an anonymous class. In Java, anonymous classes keep an implicit reference to the outer class.
If we do this:
CountDownTimer countDownTimer = new CountDownTimer(10000, 1000) { ... }.start();
countDownTimer = new CountDownTimer(10000, 1000) { ... }.start();
This causes a memory leak, because when the GC will try to collect the first instance of CountDownTimer, it will not be able to destroy it, as the anonymous class has a reference.
This can be fixed creating a static handler class (static classes do not hold any implicit reference to their outer class). In this case, we need to create a WeakReference of the outer class, because we need to acces to some of its properties.
Change-Id: I862d25ee697e55304a7f347ae9e3b5c9869017b9
1 file changed