Skip to content

Instantly share code, notes, and snippets.

@ysoftware
Last active March 13, 2017 12:05
Show Gist options
  • Save ysoftware/989b7530bee1ba3a4d2e73397fb9d877 to your computer and use it in GitHub Desktop.
Save ysoftware/989b7530bee1ba3a4d2e73397fb9d877 to your computer and use it in GitHub Desktop.
Global class that keeps an object in memory. Can be used to pass objects to a new Activity instead of using Intent. Will not work if Intent is in a different process. Overall this is a very fragile system and will fail at any given chance.
import android.support.annotation.Nullable;
@SuppressWarnings("unchecked")
final public class Global {
private static final Global shared = new Global();
private Global() {}
private Object object;
// returns the object, if expected class matches
@Nullable static public <T> T get(Class<T> kind) {
T output = null;
if (shared.object.getClass().equals(kind)) {
output = (T)shared.object;
}
return output;
}
static void remove() { shared.object = null; }
static void put(Object obj) { shared.object = obj; }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment