background preloader

Learn

Facebook Twitter

Android lifecycle. Layout Inflation as Intended. It’s common practice in the Android SDK, but you may be surprised to find that there is a wrong way to use LayoutInflater, and your application might be one of the offenders.

Layout Inflation as Intended

If you’ve ever written something like the following code using LayoutInflater in your Android application: PLEASE read on, because you’re doing it wrong and I want to explain to you why. Get to Know LayoutInflater Let’s first take a look at how LayoutInflater works. There are two usable versions of the inflate() method for a standard application: The first parameter points to the layout resource you want to inflate. It is these last two parameters that can cause a bit of confusion. Many developers take this behavior to mean that the proper way to disable attachment on inflation is by passing null as root; in many cases not even realizing that the three parameter version of inflate() exists.

Examples from the Framework So why do you suppose we are given this ViewGroup if we are not supposed to attach to it? Hooray! Context, What Context? Context objects are so common, and get passed around so frequently, it can be easy to create a situation you didn’t intend.

Context, What Context?

Loading resources, launching a new Activity, obtaining a system service, getting internal file paths, and creating views all require a Context (and that’s not even getting started on the full list!) To accomplish the task. What I’d like to do is provide for you some insights on how Context works alongside some tips that will (hopefully) allow you to leverage it more effectively in your applications. Context Types Not all Context instances are created equal. Application – is a singleton instance running in your application process. Activity/Service – inherit from ContextWrapper which implements the same API, but proxies all of its method calls to a hidden internal Context instance, also known as its base context.

BroadcastReceiver – is not a Context in and of itself, but the framework passes a Context to it in onReceive() each time a new broadcast event comes in. Custom Layouts on Android. If you ever built an Android app, you have definitely used some of the built-in layouts available in the platform—RelativeLayout, LinearLayout, FrameLayout, etc.

Custom Layouts on Android

They are our bread and butter for building Android UIs. The built-in layouts combined provide a powerful toolbox for implementing complex UIs. But there will still be cases where the design of your app will require you to implement custom layouts. There are two main reasons to go custom. First, to make your UI more efficient—by reducing the number of views and/or making faster layout traversals. In this post, I will demonstrate four different ways of implementing custom layouts and discuss their respective pros and cons: composite view, custom composite view, flat custom view, and async custom views.

The code samples are available in my android-layout-samples repo. Ok, let’s start with the most common type of custom layout: composite view. Composite View This is usually your starting point. Custom Composite View Flat Custom View. How Garbage Collection works in Java.