Skip to content

Instantly share code, notes, and snippets.

@ahem
Created April 29, 2014 19:33
Show Gist options
  • Save ahem/e8472dc653bddb30de0e to your computer and use it in GitHub Desktop.
Save ahem/e8472dc653bddb30de0e to your computer and use it in GitHub Desktop.
Android parcelling test
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="com.issuu.parcelerarraytest.app.MainActivity">
<TextView
android:id="@+id/text_view"
android:text="@string/hello_world"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
package com.issuu.parcelerarraytest.app;
import android.app.Activity;
import android.os.Parcelable;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
import org.parceler.Parcel;
import org.parceler.ParcelConstructor;
import org.parceler.ParcelerRuntimeException;
import org.parceler.Parcels;
import java.lang.reflect.Field;
import java.util.Arrays;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
IntArrayClass prewrap = new IntArrayClass(10, new int[] { 1,2,3,4 });
IntArrayClass unwrapped = Parcels.unwrap(wrap(prewrap));
if (unwrapped.arr != null && unwrapped.arr.length == 4
&& Arrays.toString(prewrap.arr).equals(Arrays.toString(unwrapped.arr))) {
((TextView)findViewById(R.id.text_view)).setText(Arrays.toString(unwrapped.arr));
} else {
((TextView)findViewById(R.id.text_view)).setText("failed :-(");
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Parcel(Parcel.Serialization.METHOD)
public static class IntArrayClass {
int[] arr;
int i;
@ParcelConstructor
public IntArrayClass(int i, int[] arr) {
this.arr = arr;
this.i = i;
}
public IntArrayClass() {}
public int[] getArr() { return arr; }
public void setArr(int arr[]) { this.arr = arr; }
public int getI() { return i; }
public void setI(int i) { this.i = i; }
}
public static Parcelable wrap(Object input){
try{
android.os.Parcel parcel = android.os.Parcel.obtain();
Parcelable parcelable = Parcels.wrap(input);
parcelable.writeToParcel(parcel, 0);
Field creatorField = parcelable.getClass().getField("CREATOR");
return (Parcelable)((Parcelable.Creator)creatorField.get(parcelable)).createFromParcel(parcel);
} catch (IllegalAccessException e) {
throw new ParcelerRuntimeException("IllegalAccessException", e);
} catch (NoSuchFieldException e) {
throw new ParcelerRuntimeException("NoSuchFieldException", e);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment