1

I have this custom object which I want to pass to a different Activity:

public class FindRouteOutputForDisplay {
    public ArrayList<VertexDisplay> vertexDisplayArrayList;
    public ArrayList<Integer> integerArrayList;
}

I tried this, but doesn't work.

Intent newIntent = new Intent(FirstActivity.this, SecondActivity.class);
FindRouteOutputForDisplay data = getMyData();
newIntent.putExtra("data", data); // Error
startActivity(newIntent);

Should I pass

    public ArrayList<VertexDisplay> vertexDisplayArrayList;
    public ArrayList<Integer> integerArrayList;

separately one-by-one? Any way to pass all of them at once?

Joon. P
  • 2,238
  • 7
  • 26
  • 53

8 Answers8

1

You can try this solution.

public class FindRouteOutputForDisplay implements Serializable{
   public ArrayList<VertexDisplay> vertexDisplayArrayList;
   public ArrayList<Integer> integerArrayList;
}

Intent newIntent = new Intent(FirstActivity.this, SecondActivity.class);
FindRouteOutputForDisplay data = getMyData();
newIntent.putSerializable("data", data); 
startActivity(newIntent);

If you have any errors on "putSerializable" so you can cast your object to Serializable in this way.

newIntent.putSerializable("data", (Serializable)data);

And in your activity just get your data by this

FindRouteOutputForDisplay data = getIntent().getSerializable("data");

Hope it helps.

Roman Pozdnyakov
  • 408
  • 1
  • 4
  • 17
0

Save your data in your application context class. Since this class is available across your application instance, you can get or set the variables defined inside this class from all activities. More info here

Febi M Felix
  • 2,799
  • 1
  • 10
  • 13
0

Change this :

startActivity(routeDetailIntent);

To :

startActivity(newIntent);
emme
  • 21
  • 3
0

You should make the custom objects implement the Parcelable interface:

public class MyParcelable implements Parcelable {
    private int mData;

     public int describeContents() {
         return 0;
     }

     public void writeToParcel(Parcel out, int flags) {
         out.writeInt(mData);
     }

     public static final Parcelable.Creator<MyParcelable> CREATOR
         = new Parcelable.Creator<MyParcelable>() {
         public MyParcelable createFromParcel(Parcel in) {
             return new MyParcelable(in);
         }

         public MyParcelable[] newArray(int size) {
             return new MyParcelable[size];
         }
     };

     private MyParcelable(Parcel in) {
         mData = in.readInt();
     }
}

You can also have a look here if you want other solutions.

Community
  • 1
  • 1
Quintin Balsdon
  • 5,484
  • 10
  • 54
  • 95
  • from "private MyParcelable(Parcel in) { mData = in.readInt(); }", How can I use it for multiple class variables? – Joon. P Jan 12 '17 at 11:13
  • You can write many variables into the parcel - you must just make sure the in order and out order are the SAME (https://guides.codepath.com/android/using-parcelable) – Quintin Balsdon Jan 12 '17 at 11:15
0

you have passed wrong intent to start activity.

Intent newIntent = new Intent(FirstActivity.this, SecondActivity.class);
Bundle bundle = new Bundle();
bundle.putSerializable("value", data );
newIntent.putExtras(bundle);
startActivity(newIntent );
santosh kumar
  • 2,952
  • 1
  • 15
  • 27
0

I have a trick for you. Add Gson to your dependencies.

repositories {
    mavenCentral()
}

dependencies {
    compile 'com.google.code.gson:gson:2.2.4'
}

Convert your object to Json and ship it as a String.

String data = new Gson().toJson(getMyData())

Convert it back from String in a new Activity

FindRouteOutputForDisplay data = new Gson(stringData, FindRouteOutputForDisplay.class).
Andrej Jurkin
  • 2,216
  • 10
  • 19
0

I also face this problem so I'm trying this below code:

Pass the object with intent

In starting activity we can set the parcelable object to an intent

User user = new User("1", "u", "u", "u");

ArrayList<User> userList = new ArrayList<User>();

Sensor sensor = new Sensor("1", "s", "s", true, user, userList);

intent.putExtra("com.score.senzors.pojos.Sensor", sensor);

In destination activity we can get the parcelable object from intent extras(bundle)

Bundle bundle = getIntent().getExtras();

Sensor sensor = bundle.getParcelable("com.score.senzors.pojos.Sensor")

Hope it Helps you

santosh kumar
  • 2,952
  • 1
  • 15
  • 27
Sejal Baraiya
  • 238
  • 2
  • 9
0

You can make the class static and use it anywhere in your app.

emme
  • 21
  • 3