CN

Android Tips Round-Up, Part 4

Word count: 897Reading time: 5 min
2015/03/15
loading

In the original Chinese post, I translated Dan Lew’s fourth Android tips round-up, which he published in May 2014. These are small tools worth sharing, and I added my own notes where I had something to contribute. The version updates are noted below.

Technical review — 2026-10-13: Most of these View/XML APIs remain usable, but the explanations of configuration changes, clipping, and atomic files needed correction. The Gradle Daemon has been enabled by default since 3.0. AtomicFile behavior is illustrated using Android 4.4.4 and Android 16 source versions below.

Activity.isChangingConfigurations() — Available since API 11, this identifies an Activity being destroyed because its configuration is changing. It does not save state or replace appropriate saved-instance-state, ViewModel, or persistent storage handling. A false result at an arbitrary moment does not rule out later recreation; an already stopped Activity can still be recreated after a configuration change.

SearchRecentSuggestionsProvider — Supplies recent-query suggestions for the system search framework. Subclass it, call setupSuggestions() from the constructor, and keep the authority consistent with the manifest and searchable XML. The app also needs to save queries with SearchRecentSuggestions.saveRecentQuery(); the provider does not automatically capture all input or create the search interface.

ViewTreeObserver — Dan Lew often used this to wait for Views to be measured before starting animations. It observes global layout, pre-draw, and related events, letting you read dimensions after layout. It does not actively measure a View. Callbacks can repeat, so remove a listener when only one notification is needed. Check isAlive() before using a long-lived observer reference; a dead observer cannot be operated on.

org.gradle.daemon=true — Reusing a JVM can reduce command-line build startup overhead. However, the Daemon is enabled by default from Gradle 3.0, so modern projects generally do not need this extra line to enable it. Daemons, incremental builds, and the build cache are separate mechanisms; repeating a setting is not a new optimization.

DatabaseUtils — Provides helpers for Cursor inspection, queries, and ContentValues conversion. For example, cursorRowToContentValues() works on the current row, while dumpCursor() helps with debugging. The caller still owns resource-lifetime decisions and must close Cursors as appropriate.

android:weightSum (LinearLayout) — Sets the total weight used to distribute remaining space; otherwise LinearLayout sums its children’s layout_weight values. A larger explicit total can leave some space unallocated. Base child sizes and parent measurement constraints still affect the result, so a weight is not automatically a percentage of the screen.

android:duplicateParentState (View) — Uses the direct parent’s drawable state, such as pressed or focused. It does not copy every parent property or install click listeners.

android:clipChildren (ViewGroup) — Defaults to true and clips each child to that child’s own bounds. Setting it to false permits drawing beyond those bounds, but ancestors may still clip the result. clipToPadding is a separate setting. Drawing outside a View does not automatically enlarge its layout or touch target.

android:fillViewport (ScrollView) — Stretches a short direct child to fill the ScrollView’s available viewport height, not necessarily the entire screen. Taller content still scrolls. A ScrollView permits only one direct child; use a container when several controls are needed.

android:tileMode (BitmapDrawable) — Chooses bitmap tiling: repeat repeats the image, mirror alternates mirrored copies, and clamp extends edge colors. The default is disabled. Enabling tiling ignores gravity; this is separate from ImageView scaling.

android:enterFadeDuration / android:exitFadeDuration — From API 11, these control the fade durations when a DrawableContainer switches child Drawables, such as state-list transitions. Values are milliseconds of fading, not a delay before displaying content or an automatic entrance animation for the whole View.

android:scaleType (ImageView) — Controls Drawable scaling and cropping inside the content area. centerCrop preserves aspect ratio and covers the area, potentially cropping edges; centerInside preserves the ratio while fitting the whole image inside. This affects presentation, not automatic downsampling of a large source bitmap during decoding.

<merge> — As a layout root, it inserts children directly into an existing parent, avoiding an unnecessary ViewGroup. <include> performs layout-file inclusion; <merge> itself is not a View. Inflating it requires a valid parent and immediate attachment. Preserve any required background, padding, or interaction role before removing a container.

AtomicFile — Dan Lew had written similar functionality himself and was glad to find an official implementation. The framework class, available since API 17, updates a single file. Android 4.4.4 used a .bak backup; Android 16 writes a .new file before replacement and handles legacy backups. Complete startWrite() with finishWrite() or failWrite() instead of merely closing its stream. It provides no inter-thread or interprocess file locking: the caller must enforce mutual exclusion for access.

More

  1. Part 1
  2. Part 2
  3. Part 3
  4. Part 4
  5. Part 5

Original Article: https://blog.danlew.net/2014/05/12/android-tips-round-up-part-4/

About Me && Blog

Below is my personal intro and related links. I look forward to exchanging ideas with fellow professionals. “When three walk together, one can always be my teacher!”

  1. Blogger Intro
  2. Blog Content Navigation: A guide for my blog content.
  3. Curated Excellent Blog Articles - Android Performance Optimization Must-Knows
  4. Android Performance Optimization Knowledge Planet: The paid community stopped operating in 2026 and no longer accepts new members; this link preserves its history.

One walks faster alone, but a group walks further together.

Scan WeChat QR Code

CATALOG
  1. 1. More
  • About Me && Blog