Patterns for running Asynchronous code in Android (Part 1)

An important technique in Android Programming is to make potentially long task run asynchronously in your app, avoid blocking the UI thread, so your app feels more responsive. For example, downloading a file from the Internet, loading a media file from the external storage or applying a filter on an image bitmap etc. I will show you how to implement with AsyncTask, simple java threads and java Executor in Part 1.

Let’s say I want to load a bitmap from the internet, and set it to an ImageView , this might be what you’d write at first:

The Java way: simple Threads

You can implement it just like any other Java programs, define a Thread that does the job, and run it. Every Java programmer knows how to do that. Remember that if you want to modify the UI, you need to use runOnUiThread()

Continue reading

[Android] Using LruCache to cache Bitmaps and limit memory usage

Bitmaps are very bulk objects, loading them from the Internet or local storage could be expensive and “choke” the UI. You want to  keep them in memory so that they could be loaded instantly, however you also want to discard them as soon as they are not useful. Here comes the LruCache.

What is LruCache?

LruCache is a util introduced in API 12, which is an implementaion of the Least Recently Used cache. In short, that is a caching strategy which the “oldest” entries are discarded when overflow occur. It is very useful to cache Bitmaps. The default implementation allows you to limits the number of items in the cache. Here’s how to slightly modify the LruCache to limit the memory usage of the cache.

How to use it?

Well, actually it act just like a HashMap. Continue reading

What is Git and how to use Git in your own project?

What is Git?

In short, Git is a free and open-source distributed version control system. If you have used CVS/SVN, they are centralized repositories, in contrast, Git is the “distributed” version. It was created by Linus Torvalds himself as a tool to manage the Kernel. He has a very nice Google Talk about Git, which I recommend you to check it out first. If you have had some experience using SVN, you can easily understand. I’m going to include some summary of the video in this article.

Why Git?

  1. It is distributed, and it is the only right way to do version control (as Linus say so). Well, actually it means every one owns a full copy of the repository and no single repo is more important than the others. Collaboration happens by pulling and pushing code between repos and branches.
  2. Branching is inherent property of development. Continue reading