Posts

Showing posts from March, 2017

Smart way to update RecyclerView using DiffUtil

Image
Say Good Bye To notifyDataSetChanged() We generally use List in any of our day to day development. It also requires to update the list data when user scrolls the list. To achieve this, we often fetch the data from server and update the newly received items. If there is a small delay during this process it impact on the user experience so we want this to be done as soon as possible with along with less resources. When the content of our list gets changed, we have to call notifyDataSetChanged() to get the updates but it is very costly. There are so many iterations for getting the job done in the case of notifyDataSetChanged(). Here DiffUtil class comes into picture and Android developed this utility class to handle data updates for a RecyclerView. What is DiffUtil As of 24.2.0, RecyclerView support library, v7 package offers really handy utility class called DiffUtil. This class finds the difference betwe...

Android Performance: Avoid using ENUM on Android

Image
What and why we use ENUM? Enum in java is a data type that contains fixed set of constants. When we required predefined set of values which represents some kind of data, we use ENUM. We always use Enums when a variable can only take one out of a small set of possible values. For example: public enum Season {   WINTER, SPRING, SUMMER, FALL } Instead of using Integer or String constants, we generally use ENUM to increase compile time checking and avoid errors from passing in invalid constants. Drawback of using ENUM Each value in an ENUM is an object and each declaration will take some runtime memory simply to reference the object. So ENUM values will take more memory then Integer or String constant. Even in old android devices (<=2.2), there were some performance issue related to ENUM which were solved in JIT compiler. Adding a single ENUM will increase the size (13x times than the Integer constant) of final DEX file. It also generates th...

App optimization with ArrayMap and SparseArray in Android

Image
Collections are the most common thing used in the software development. In general whenever we required to store data in key value pairs, the very first data structure that comes to our mind is HashMap. It is quite flexible hence it is the most preferred data structure choice to store key value pairs. To improve the performance of Android application, Android system provides set of collections build especially for mobile development. So ArrayMap and SparseArray are intended to be more memory efficient than using a HashMap. How HashMap works? HashMap is basically an Array of HashMap.Entry objects. Entry is inner class of HashMap which holds the key and values. When a key-value pair is inserted into a HashMap, a hashCode of the key is calculated, and that value is assigned to the hashCode variable of EntryClass. Now using hashCode, we can get the index of the bucket where it will be stored. In case bucket has a pre-existing element, the new element is inserted...

Android Dev Tool: Best practice with StrictMode

Image
StrictMode (android.os.StrictMode) is a developer tool which detects things you might be doing wrong by accidently and brings them to your attention. Best practice in Android says “keeping the disk and network operations off from the main thread makes applications much smoother and more responsive”.  So StrictMode is use to catch the coding issues such as disk I/O or network access on the application's main thread i.e. UI thread. This is a debugging tool introduced in Android 2.3(API level 9) but more features were added in Android 3.0. StrictMode Policies StrictMode has two types of policies and each policy has various rules. 1.     Thread policy 2.     VM policy Thread Policy- Thread policies are focused on the things which is not recommended to do in main thread like disk or network operations. The thread policy can monitor following violations: ·        Disk Reads · ...