Photo I Love

Kevin Chen

Welcome to my home on the internet! Everything here is free under the Creative Commons Attribution 3.0 license unless marked otherwise.

This site contains various pieces of writing across my various interests, and spanning several years. You can fork this site on github if you wish.

Android ViewHolder Pattern

  • Viewholder模式在Adapter中有广泛的应用, 一般的写法是在Adapter类中写一个静态内部类,
  • 里面包含几个静态成员变量。这里介绍的写法很酷。 但是执行效果跟普通做法基本一致。本类中用到了
  • 效率比较高的集合类SparseArray.也是Android里 推荐用它类替换HashMap<Integer,Object>。
(read more...)

The Model-View-Presenter pattern

You’ve most likely heard of the MVC (Model-View-Controller) pattern, and you’ve probably used it in different frameworks. When I was trying to find a better way to test my Android code, I learned about the MVP (Model-View-Presenter) pattern. The basic difference between MVP and MVC is that in MVP, the presenter contains the UI business logic for the view and communicates with it through an interface. In this hack, I’ll show you how to use MVP inside Android and how it improves the testability of the code. To see how it works, we’ll build a splash screen. A splash screen is a common place to put initialization code and verifications, before the application starts running. In this case, inside the splash screen we’ll provide a progress bar while we’re checking whether or not we have internet access. If we do, we continue to another activity, but if we don’t, we’ll show the user an error mes- sage to prevent them from moving forward.

(read more...)

Android Async HTTP Clients: Volley vs Retrofit

We recently released a new version of our mobile app for Android. Although it was a huge improvement from previous installments in terms of features, design, and usability, there was one nagging issue in the back of our minds: speed. There are times when the app isn’t as snappy as we’d like it to be. After some profiling, benchmarks, and common sense, we determined that retrieving data from the API (the networking) was the bottleneck.

(read more...)

Android LocalBroadcastManager

Helper to register for and send broadcasts of Intents to local objects within your process. This is has a number of advantages over sending global broadcasts with {@link android.content.Context#sendBroadcast}:

  • You know that the data you are broadcasting won’t leave your app, so don’t need to worry about leaking private data.
  • It is not possible for other applications to send these broadcasts to your app, so you don’t need to worry about having security holes they can exploit.
  • It is more efficient than sending a global broadcast through the system.
(read more...)