Skip to content

Instantly share code, notes, and snippets.

View kpgalligan's full-sized avatar

Kevin Galligan kpgalligan

View GitHub Profile
Using "layout-w600p" and similar can be dangerous. Rotating some devices will cause a different layout resource to be used, and if a View class is different, there may be a ClassCastException on restore. Use "laout-sw600p".
http://stackoverflow.com/questions/14880746/difference-between-sw600dp-and-w600dp
Caused by: java.lang.ClassCastException: org.lucasr.twowayview.TwoWayView$SavedState cannot be cast to android.widget.AbsListView$SavedState at android.widget.AbsListView.onRestoreInstanceState(AbsListView.java:1833) at android.view.View.dispatchRestoreInstanceState(View.java:12231) at android.view.ViewGroup.dispatchThawSelfOnly(ViewGroup.java:2613) at android.widget.AdapterView.dispatchRestoreInstanceState(AdapterView.java:790)
@kpgalligan
kpgalligan / gist:6584675
Last active December 23, 2015 05:09 — forked from jrm2k6/gist:6584648
import System.Environment (getArgs)
stringToArray :: String -> [String]
stringToArray s = words s
findLongestWord :: String -> [String] -> String
findLongestWord x [] = x
findLongestWord l (w1:s1)
| length w1 > length l = findLongestWord w1 s1
| otherwise = findLongestWord l s1

Threading patterns

To understand mobile, you need to understand threading. If you don't really get that, you'll wind up in various worlds of hurt.

Methods

AsyncTask

AsyncTask is probably the first thing everybody uses. Its all over the early examples in Android. Its pretty basic, assuming you don't hit the methods nobody ever uses. Just "doInBackground" and "postExecute". The idea is that you do something in "doInBackground", in a background thread, and in "postExecute", you do something in the main thread. Generally, tweak your UI.

@kpgalligan
kpgalligan / SignUpFragment.java
Created September 30, 2014 17:01
String matching for errors is brittle and likely to break
} else if (task.userResponse.getMessage().equals("It appears you've already signed up")) {
Toast.makeText(getActivity(), "It appears that you've already signed up", Toast.LENGTH_SHORT).show();
}
@kpgalligan
kpgalligan / gist:1a0d4851bd9118e8a589
Last active August 29, 2015 14:07
Item 1: Consider static factory methods instead of constructors

The normal way for a class to allow a client to obtain an instance of itself is to provide a public constructor. There is another technique that should be a part of every programmer’s toolkit. A class can provide a public static factory method, which is simply a static method that returns an instance of the class. Here’s a simple example from Boolean (the boxed primitive class for the primitive type boolean). This method translates a boolean primitive value into a Boolean object reference:

public static Boolean valueOf(boolean b) { return b ? Boolean.TRUE : Boolean.FALSE; }

Note that a static factory method is not the same as the Factory Method pattern from Design Patterns [Gamma95, p. 107]. The static factory method described in this item has no direct equivalent in Design Patterns.

A class can provide its clients with static factory methods instead of, or in addition to, constructors. Providing a static factory method instead of a public constructor has both advantages and disadvantages.

@kpgalligan
kpgalligan / gist:143fd33011c402c38e95
Created October 11, 2014 16:06
Item 2: Consider a builder when faced with many constructor parameters

Static factories and constructors share a limitation: they do not scale well to large numbers of optional parameters. Consider the case of a class representing the Nutrition Facts label that appears on packaged foods. These labels have a few required fields—serving size, servings per container, and calories per serving—and over twenty optional fields—total fat, saturated fat, trans fat, cholesterol, sodium, and so on. Most products have nonzero values for only a few of these optional fields.

What sort of constructors or static factories should you write for such a class? Traditionally, programmers have used the telescoping constructor pattern, in which you provide a constructor with only the required parameters, another with a single optional parameter, a third with two optional parameters, and so on, culminating in a constructor with all the optional parameters. Here’s how it looks in practice. For brevity’s sake, only four optional fields are shown:

Code View: Scroll / Show All // Telescoping constructor

@kpgalligan
kpgalligan / gist:d642e148291a4ed47def
Last active August 29, 2015 14:07
Item 5: Avoid creating unnecessary objects

It is often appropriate to reuse a single object instead of creating a new functionally equivalent object each time it is needed. Reuse can be both faster and more stylish. An object can always be reused if it is immutable (Item 15).

As an extreme example of what not to do, consider this statement:

String s = new String("stringette"); // DON'T DO THIS!

The statement creates a new String instance each time it is executed, and none of those object creations is necessary. The argument to the String constructor ("stringette") is itself a String instance, functionally identical to all of the objects created by the constructor. If this usage occurs in a loop or in a frequently invoked method, millions of String instances can be created needlessly.

The improved version is simply the following:

@kpgalligan
kpgalligan / gist:b0950ff433708b57ca0c
Last active August 29, 2015 14:07
Using serializable and parcelable

In general, avoid both. We should be using EventBus where possible, and passing arguments to Fractivities by reference rather than value.

However, saving state may need a more complex object, and you may need to pass data to Fractivities that can't be done with a reference.

In almost all cases, use Serializable. Be careful to write tests that actually attempt to serialize these objects. They won't actually be serialized until they need to be, which may not work if not tested.

Parcelable is "faster", but requires lots of explicit code that can be done incorrectly. Its also easy to forget updating the state manipulation methods when updating the objects themselves.

Comparison http://www.developerphil.com/parcelable-vs-serializable/

@kpgalligan
kpgalligan / gist:4972b681addbd072d413
Created October 11, 2014 16:20
compareTo needs heavy test coverage

Unless the method is extremely simple, 'compareTo' needs heavy unit test coverage. Its very easy to mess this up, and hard to visually spot errors.

@kpgalligan
kpgalligan / gist:73ee19719ebc97928758
Last active August 29, 2015 14:07
Testing Setup

There are many testing libraries for Android, and we'll be adding more as time goes on, but for now we'll focus on the following.

  1. Standard JUnit (v3) - This comes with Android
  2. Android JUnit - This also comes with Android, and lets you do stuff with the context and lifecycle.
  3. Espresso - Built on top of the Android JUnit. Makes instrumentation usable.

Eventually we'll probably get into dependency injection, mocking, etc, but for now I'd like to focus on relatively macro testing.

What to test