For the second Chinese post in this series, I translated Dan Lew’s Android Tips Round-Up. As in the first part, I’ve added notes from using some of these tools and filled in explanations where I thought they would help. If a few of the APIs are new to you, I hope the list comes in handy. The API updates are noted below.
Reviewed 2026-10-13: This 2014 list covers View and system utility APIs.
Activity.onBackPressed()was deprecated in API 33, and text layout has a Builder API from API 23. The notes below add replacements and correct the explanations of alarms, application memory, and screen density.
DateUtils.formatDateTime() formats an epoch-millisecond timestamp using local date/time conventions. Flags choose which date, weekday, or time components to display. Use it for user-facing text rather than a fixed data-exchange format.
AlarmManager.setInexactRepeating() lets Android adjust repeating-alarm delivery times to batch wakeups. It does not automatically call cancel(); the app cancels alarms when no longer needed. From Android 4.4 (API 19), repeating alarms for apps targeting API 19 or later are inexact. RTC does not wake a sleeping device, while RTC_WAKEUP requests a wakeup; power-management policies still apply. An eight-hour sleep does not establish a guaranteed sixteen-to-one delivery rule or a fixed energy saving. Use a one-shot alarm for a single event. See the alarm scheduling guide for task-scheduling and exact-alarm boundaries.
Formatter.formatFileSize() converts a byte count into localized display text. Android 8.0 (API 26) and later use SI units, such as 1000 bytes per kB; older releases use powers of 1024. Do not parse the formatted result back into an exact byte count.
ActionBar.hide() / show() hide or show the application’s ActionBar. They do not hide the system status and navigation bars and therefore do not by themselves enable immersive fullscreen. A standalone Toolbar also needs handling appropriate to that component.
Linkify.addLinks() adds URLSpans to text matching a type mask or regular expression. When using the Spannable overload and displaying the result in a TextView, configure an appropriate MovementMethod for link clicks.
StaticLayout lays out, measures, and draws multiline text in a custom View. From Android 6.0 (API 23), prefer StaticLayout.Builder: supply the text, TextPaint, and width, set optional properties, then call build(). Rebuild the layout when the text or width changes.
Activity.onBackPressed() is the legacy back-event hook and was deprecated in Android 13 (API 33). Use OnBackPressedDispatcher / OnBackPressedCallback with AndroidX Activity 1.6.0 or later, or the platform’s API 33 OnBackInvokedCallback. The original advice to simply override onBackPressed() needs migration; see the predictive back guide.
GestureDetector recognizes taps, long presses, scrolling, and flings from MotionEvents. Feed events into onTouchEvent(). With SimpleOnGestureListener, onDown() normally needs to return true for the rest of the gesture to be handled. See common gesture detection.
DrawFilter can temporarily modify Paint properties used by drawing operations on the Canvas where it is installed, including antialiasing flags. It does not replace onDraw() or let a child View automatically change every ancestor and descendant’s rendering. For custom drawing, configure the Paint you own directly where possible.
ActivityManager.getMemoryClass() reports an approximate per-application memory class in MB, useful for budgeting the managed heap and caches. It is neither available device RAM nor the process’s current memory use. Native allocations and graphics buffers need separate observation.
ViewStub defers layout inflation. Calling inflate() or setting visibility to VISIBLE/INVISIBLE replaces the stub with the inflated hierarchy; keep the returned root View for subsequent operations. A <merge> root is not supported for the inflated layout. See loading views on demand.
SystemClock.sleep() blocks the current thread for the requested number of uptime milliseconds while preserving its interrupted state. Deep sleep is excluded from that clock, and scheduling can delay the return further. It can simulate a delay in a background debugging task, but it does not make work asynchronous and should not block the UI thread or a task that must promptly respond to cancellation.
DisplayMetrics.density is the logical dp-to-px scale factor, not physical screen dpi; typically px = dp × density. It is a field, not a method. Use resources from the current UI Context instead of fixing the value to one device.
Pair.create() constructs a Pair<A, B> whose values are available through first and second. It is a two-value container factory; a named type is usually clearer when the data has specific business meaning.
More
Original Article: https://blog.danlew.net/2014/04/14/android-tips-round-up-part-2/
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!”
- Blogger Intro
- Blog Content Navigation: A guide for my blog content.
- Curated Excellent Blog Articles - Android Performance Optimization Must-Knows
- 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.
