Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@chkn
Created May 18, 2013 22:33
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 chkn/5605979 to your computer and use it in GitHub Desktop.
Save chkn/5605979 to your computer and use it in GitHub Desktop.
using System;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using Android.OS;
using Android.App;
using Android.Content;
public static class AndroidExtensions {
static BinaryFormatter binaryFormatter;
public static void PutObject (this Bundle bundle, string key, object val)
{
if (binaryFormatter == null)
binaryFormatter = new BinaryFormatter ();
using (var stream = new MemoryStream ()) {
binaryFormatter.Serialize (stream, val);
bundle.PutByteArray (key, stream.ToArray ());
}
}
public static object GetObject (this Bundle bundle, string key)
{
if (binaryFormatter == null)
binaryFormatter = new BinaryFormatter ();
var array = bundle.GetByteArray (key);
if (array == null)
return null;
using (var stream = new MemoryStream (array, false))
return binaryFormatter.Deserialize (stream);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment