Android – confirm preference change

If you’re using the Preferences API in Android, and want to confirm (show a dialog, perform a test, etc) before a Preference is updated, you need to find that Preference item and call setOnPreferenceChangeListener to a custom listener. This can work in conjunction with PreferenceManager.registerOnSharedPreferenceChangeListener, you just need to return false to have the Preference […]

Android – isTaskRoot returning false incorrectly

Some Launchers will launch an app at it’s LAUNCH activity on top of the last point the user was at. Imagine: 1. User opens app. 2. The Launcher Activity is designated at Activity A, so that’s what opens. 3. User navigates several Activities deep and ends up at Activity D (A > B > C […]

Android – Restart App

The title isn’t exactly accurate – you can’t “restart” an app per se – but you can get pretty close:

This starts the Activity specified in your manifest as the “LAUNCHER”, in a new task stack (history), and clear the existing stack.

Android – get system defined ActionBar size (height)

In xml, you can use ?attr/actionBarSize, but if you need access to that value in Java…

Android – View traversal / walker / finder utilities

Android – LinearLayout with dividers (separators)

Add (or amend) your values/attrs.xml to include:

Some layout:

And the class:

Android – launch modes and multiple intents

The behavior here isn’t quite what I expected, or gather from the documentation. If you have <activity android:name=”.SomeActivity” android:launchMode=”singleTop” /> Then you start SomeActivity multiple times:

You’ll get only one instance of SomeActivity at the top of the stack (as the singleTop launch mode promises), but you’ll notice that if you finish() or hit […]

Android – get default text size

Possibly useful in precomputing layout requirements…

Java – Unexpected Bounds when extending a typed Class

When extending a typed class without specifying the type, just pass the type to the subclass’s erasure and specify that type in the reference to the superclass. E.g.,

As an example, consider the AOSP class RecyclerView.Adapter, which is declared as:

A normal concrete implementation is fairly straightforward:

But what if you want […]

Java – Super Class constructor fired before SubClass members are initialized

Probably unexpected behavior:

There’s not a direct way to work around this – often initialization or setup methods are used, sometimes with a Factory instance generator.