Code Monkey home page Code Monkey logo

countdowntimer's Introduction

CountDownTimer

Custom implementation of Android's CountDownTimer

Differences over the original implementation

  • New empty constructor and start(long millisInFuture, long countDownInterval) method to change the onTick(long millisUntilFinished) interval and the time until the countdown is done.
  • Static handler class to prevent memory leaks.

More information about handler's memory leaks can be found in:

Android

Also you can cherry-pick my commit (https://github.com/pbr1111/platform_frameworks_base/commit/4e093d4c450ecdcc1e8830a104379c80d2ea1df6), fixing the memory leak in the android/platform/frameworks/base project. This commit it's based on the original implementation and does not include the new contructor and start method.

In the first video, it can be see the original implementation of the class CountDownTimer.java, using a anonymous handler class.

When I rotate the device, MainActivity is destroyed and another instance is created. MainActivity.onCreate(Bundle) is called every time, creating another instance of the CountDownTimer class and it assigns this instance in the variable countDownTimer.

If I force the GC process, in theory the previous referenced instances of the CountDownTimer should be destroyed (nobody references this instances).

In the original implementation, this does not work this way, because the handler have a reference of the outer class. This causes a memory leak (it lasts until the countdown ends).

With the fixed implementation (static handler class), although if I destroy and recreate many times MainActivity, when I force the GC, unreferenced instances will be destroyed.

countdowntimer's People

Contributors

pbr1111 avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.