Skip to content

Instantly share code, notes, and snippets.

@arriolac
Last active August 29, 2015 14:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arriolac/a66515bad322db0ce747 to your computer and use it in GitHub Desktop.
Save arriolac/a66515bad322db0ce747 to your computer and use it in GitHub Desktop.
Utility class for writing Parcelables into a Bundle (ExampleActivity.java uses the Parceler library: https://github.com/johncarl81/parceler).
import android.os.Bundle;
import android.os.Parcelable;
/**
* Utility class for {@link android.os.Bundle}.
* Created by chris on 8/6/14.
*/
public class BundleUtil {
private static final String EXTRA_PARCELABLE_COUNT = "parcelable_count";
/**
* Creates a {@link Bundle} given a list of {@link android.os.Parcelable} objects. This is a
* convenience method if all the values in the Bundle are Parcelables.
*
* @param args the {@link android.os.Parcelable} objects.
* @return the resulting {@link Bundle}.
*/
public static Bundle createBundle(Parcelable... args) {
final Bundle bundle = new Bundle();
// Put Parcelable in bundle
final int size = args.length;
for (int i = 0; i < size; i++) {
bundle.putParcelable(String.valueOf(i), args[i]);
}
// Put size in bundle (needed when using
bundle.putInt(EXTRA_PARCELABLE_COUNT, size);
return bundle;
}
/**
* NOTE: This method is used in conjunction with {@link #createBundle(android.os.Parcelable...)}.
*
* Returns an array of Parcelable objects contained within <code>bundle</code>. The returned
* array is returned in the same order they are passed in {@link #createBundle(android.os.Parcelable...)}.
*
* @param bundle the bundle
* @return an array of Parcelables contained in <code>bundle</code>.
*/
public static Parcelable[] getParcelablesFromBundle(Bundle bundle) {
// Count how many Parcelable values in the bundle
final int numOfParcelables = bundle.getInt(EXTRA_PARCELABLE_COUNT);
// Get list of Parcelables
final Parcelable[] parcelables = new Parcelable[numOfParcelables];
for (int i = 0; i < numOfParcelables; i++) {
parcelables[i] = bundle.getParcelable(String.valueOf(i));
}
return parcelables;
}
}
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Parcelable;
import com.sincerely.ink.ui.fragments.CheckoutFragment;
import com.sincerely.lib.model.addressbook.Contact;
import com.sincerely.lib.model.product.Card;
import com.sincerely.lib.network.model.BillingInfoResponse;
import java.util.List;
import org.parceler.Parcels;
/**
* Created by chris on 8/6/14.
*/
public class ExampleActivity extends Activity {
private static final String EXTRA_BUNDLE = "extra_bundle";
@Override public void onCreate(Bundle savedInstanceState) {
// Get parcelables
final Bundle bundle = getIntent().getBundleExtra(EXTRA_BUNDLE);
final Parcelable[] parcelables = BundleUtil.getParcelablesFromBundle(bundle);
final BillingInfoResponse billingInfoResponse = Parcels.unwrap(parcelables[0]);
final Card card = Parcels.unwrap(parcelables[1]);
final List<Contact> selectedContacts = Parcels.unwrap(parcelables[2]);
}
public static void launch(Activity activity,
BillingInfoResponse billingInfo,
Card card,
List<Contact> selectedContacts) {
final Intent intent = new Intent(activity, ExampleActivity.class);
intent.putExtra(EXTRA_BUNDLE,
BundleUtil.createBundle(Parcels.wrap(billingInfo),
Parcels.wrap(card),
Parcels.wrap(selectedContacts)));
activity.startActivity(intent);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment