Skip to content

Instantly share code, notes, and snippets.

@joeyjiron06
Last active December 9, 2018 18:13
Show Gist options
  • Save joeyjiron06/6679759 to your computer and use it in GitHub Desktop.
Save joeyjiron06/6679759 to your computer and use it in GitHub Desktop.
Here is an example of how to pass custom objects between activities in android. You may use a static class to hold objects, which is fine, but you can also do it the "Android" way by making your classes Parcelable. Feel free to use this code as you please.
<LinearLayout
android:id="@+id/layout_other_activity"
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=".OtherActivity"
android:orientation="vertical">
</LinearLayout>
<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=".MainActivity">
<Button
android:id="@+id/button_pass"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Pass Objects"
android:layout_centerInParent="true"
/>
</RelativeLayout>
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.widget.LinearLayout;
import android.widget.TextView;
import java.util.ArrayList;
public class OtherActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_other);
LinearLayout layout = (LinearLayout)findViewById(R.id.layout_other_activity);
ArrayList<SomeObject> someObjects = getIntent().getParcelableArrayListExtra(SomeObject.TYPE);
if (someObjects != null) {
for (SomeObject someObject : someObjects) {
TextView textView = new TextView(this);
textView.setText( someObject.someString + " " + someObject.someInt + " " + someObject.someDouble );
layout.addView(textView);
}
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.other, menu);
return true;
}
}
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import java.util.ArrayList;
public class PassingObjectsActivity extends Activity implements View.OnClickListener
{
private ArrayList<SomeObject> someObjects;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
someObjects = new ArrayList<SomeObject>();
someObjects.add(new SomeObject("object_0", 0, 0.0));
someObjects.add(new SomeObject("object_1", 1, 1.1));
someObjects.add(new SomeObject("object_2", 2, 2.2));
someObjects.add(new SomeObject("object_3", 3, 3.3));
someObjects.add(new SomeObject("object_4", 4, 4.4));
someObjects.add(new SomeObject("object_5", 5, 5.5));
someObjects.add(new SomeObject("object_6", 6, 6.6));
findViewById(R.id.button_pass).setOnClickListener(this);
}
@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 void onClick(View view) {
switch (view.getId()) {
case R.id.button_pass:
Intent intent = new Intent(this, OtherActivity.class);
intent.putParcelableArrayListExtra(SomeObject.TYPE, someObjects);
startActivity(intent);
break;
}
}
}
import android.os.Parcel;
import android.os.Parcelable;
public class SomeObject implements Parcelable
{
public String someString = "";
public int someInt = 0;
public double someDouble = 0.0;
public static String TYPE = "someObject";
public SomeObject(String s, int i, double d)
{
someString = s;
someInt = i;
someDouble = d;
}
public SomeObject(Parcel in)
{
//read in same order that you wrote in writeToParcel
someString = in.readString();
someInt = in.readInt();
someDouble = in.readDouble();
// reading in a list custom objects: in.readTypedList(someCustomObjectArrayList, someCustomObject.CREATOR )
}
@Override
public void writeToParcel(Parcel out, int i)
{
//this is the order you should read in your contructor
out.writeString(someString);
out.writeInt(someInt);
out.writeDouble(someDouble);
// writing some custom object: out.writeTypedList(someCustomObjectArrayList);
}
@Override
public int describeContents() {
return 0;
}
//for arrays and creating from a parcel
public static final Parcelable.Creator<SomeObject> CREATOR = new Parcelable.Creator<SomeObject>() {
public SomeObject createFromParcel(Parcel in) {
return new SomeObject(in);
}
public SomeObject[] newArray(int size) {
return new SomeObject[size];
}
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment