Skip to content

Instantly share code, notes, and snippets.

@kaushikgopal
Last active August 29, 2015 14:02
Show Gist options
  • Save kaushikgopal/6cca5730b806194de361 to your computer and use it in GitHub Desktop.
Save kaushikgopal/6cca5730b806194de361 to your computer and use it in GitHub Desktop.
A nice helper utility that creates your Bundle with extras quickly. Apache 2 licensed
import android.os.Bundle;
// see my gist for [https://gist.github.com/kaushikgopal/9eea148a2188dc58fe37](ParcelableSparseArray) implementation
public class Bundler {
private Bundle _bundle;
public Bundler() {
_bundle = new Bundle();
}
public Bundler(String key, String value) {
_bundle = new Bundle();
_bundle.putString(key, value);
}
// TEST: write test for this
public Bundler(String key, String[] value) {
_bundle = new Bundle();
_bundle.putStringArray(key, value);
}
// TEST: write test for this
public Bundler(String key, boolean value) {
_bundle = new Bundle();
_bundle.putBoolean(key, value);
}
public Bundler(String key, Integer value) {
_bundle = new Bundle();
_bundle.putInt(key, value);
}
public Bundler(String key, ParcelableSparseArray value) {
_bundle = new Bundle();
_bundle.putParcelable(key, value);
}
public Bundler with(String key, String value) {
_bundle.putString(key, value);
return this;
}
public Bundler with(String key, Integer value) {
_bundle.putInt(key, value);
return this;
}
public Bundler with(String key, String[] value) {
_bundle.putStringArray(key, value);
return this;
}
public Bundler with(String key, boolean value) {
_bundle.putBoolean(key, value);
return this;
}
public Bundler with(String key, ParcelableSparseArray value) {
_bundle.putParcelable(key, value);
return this;
}
public Bundle build() {
return _bundle;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment