Skip to content

Instantly share code, notes, and snippets.

@felipecsl
Last active August 29, 2015 14:17
Show Gist options
  • Save felipecsl/45bc010713c0f543354e to your computer and use it in GitHub Desktop.
Save felipecsl/45bc010713c0f543354e to your computer and use it in GitHub Desktop.
Convenience classes for Fragment instantiation/setting arguments and chaining Bundle calls. MIT license
import android.os.Bundle;
import android.os.Parcelable;
/**
* A Bundle that doesn't suck. Allows you to chain method calls as you'd expect.
*/
public class BundleBuilder {
private final Bundle bundle;
public BundleBuilder() {
this(new Bundle());
}
public BundleBuilder(Bundle existingBundle) {
bundle = new Bundle(existingBundle);
}
public BundleBuilder putBoolean(String key, boolean value) {
bundle.putBoolean(key, value);
return this;
}
public BundleBuilder putByte(String key, byte value) {
bundle.putByte(key, value);
return this;
}
public BundleBuilder putChar(String key, char value) {
bundle.putChar(key, value);
return this;
}
public BundleBuilder putShort(String key, short value) {
bundle.putShort(key, value);
return this;
}
public BundleBuilder putInt(String key, int value) {
bundle.putInt(key, value);
return this;
}
public BundleBuilder putLong(String key, long value) {
bundle.putLong(key, value);
return this;
}
public BundleBuilder putFloat(String key, float value) {
bundle.putFloat(key, value);
return this;
}
public BundleBuilder putDouble(String key, double value) {
bundle.putDouble(key, value);
return this;
}
public BundleBuilder putString(String key, String value) {
bundle.putString(key, value);
return this;
}
public BundleBuilder putParcelable(String key, Parcelable value) {
bundle.putParcelable(key, value);
return this;
}
public BundleBuilder putAll(Bundle bundle) {
this.bundle.putAll(bundle);
return this;
}
/**
* Converts this BundleBuilder into a plain Bundle object.
*
* @return A new Bundle containing all the mappings from the current BundleBuilder.
*/
public Bundle toBundle() {
return new Bundle(bundle);
}
}
import android.os.Bundle;
import android.os.Parcelable;
import android.support.v4.app.Fragment;
/**
* A bundle builder for fragment arguments.
*
* @param <F> The fragment type you're bundling to.
*/
@SuppressWarnings("unchecked")
public class FragmentBundleBuilder<F extends Fragment> extends BundleBuilder {
private final FragmentBundler<F> fragmentBundler;
public FragmentBundleBuilder(FragmentBundler<F> fragmentBundler) {
this.fragmentBundler = fragmentBundler;
}
@Override
public FragmentBundleBuilder<F> putAll(Bundle bundle) {
return (FragmentBundleBuilder<F>) super.putAll(bundle);
}
@Override
public FragmentBundleBuilder<F> putBoolean(String key, boolean value) {
return (FragmentBundleBuilder<F>) super.putBoolean(key, value);
}
@Override
public FragmentBundleBuilder<F> putInt(String key, int value) {
return (FragmentBundleBuilder<F>) super.putInt(key, value);
}
@Override
public FragmentBundleBuilder<F> putParcelable(String key, Parcelable value) {
return (FragmentBundleBuilder<F>) super.putParcelable(key, value);
}
@Override
public FragmentBundleBuilder<F> putLong(String key, long value) {
return (FragmentBundleBuilder<F>) super.putLong(key, value);
}
@Override
public FragmentBundleBuilder<F> putString(String key, String value) {
return (FragmentBundleBuilder<F>) super.putString(key, value);
}
@Override
public FragmentBundleBuilder<F> putFloat(String key, float value) {
return (FragmentBundleBuilder<F>) super.putFloat(key, value);
}
@Override
public FragmentBundleBuilder<F> putDouble(String key, double value) {
return (FragmentBundleBuilder<F>) super.putDouble(key, value);
}
@Override
public FragmentBundleBuilder<F> putByte(String key, byte value) {
return (FragmentBundleBuilder<F>) super.putByte(key, value);
}
@Override
public FragmentBundleBuilder<F> putChar(String key, char value) {
return (FragmentBundleBuilder<F>) super.putChar(key, value);
}
@Override
public FragmentBundleBuilder<F> putShort(String key, short value) {
return (FragmentBundleBuilder<F>) super.putShort(key, value);
}
public F build() {
return fragmentBundler.build();
}
}
import android.support.v4.app.Fragment;
/**
* Set fragment arguments like a Pro.
* @param <F> The fragment setup.
*/
public class FragmentBundler<F extends Fragment> {
private final F fragment;
private final FragmentBundleBuilder<F> bundle;
private FragmentBundler(F fragment) {
this.fragment = fragment;
bundle = new FragmentBundleBuilder<>(this);
}
public static <F extends Fragment> FragmentBundleBuilder<F> make(F fragment) {
return new FragmentBundler<>(fragment).bundle();
}
public F build() {
fragment.setArguments(bundle.toBundle());
return fragment;
}
public FragmentBundleBuilder<F> bundle() {
return bundle;
}
}
@felipecsl
Copy link
Author

FragmentBundle usage:

public static MyFragment newInstance() {
    return FragmentBundler.make(new MyFragment())
                .putParcelable(ARG_PARCELABLE, parcelableArg)
                .putLong(ARG_LONG, longArg)
                .putInt(ARG_INT, intArg)
                .putString(ARG_STRING, stringArg)
                .putBoolean(ARG_BOOLEAN, booleanArg)
                .build();
}

BundleBuilder usage:

return new BundleBuilder()
        .putLong(LONG_KEY, longArg)
        .putString(STRING_KEY, stringArg)
        .putAll(existingBundle)
        .toBundle();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment