1

I am trying to send an array of objects to a new intent, but the problem is that I don't know how to send the whole array at once. The method I managed to get it working is adding object by object, in a for loop, and in the second activity I have to get them one by one, something like this:

Main activity:

        Intent newIntent = new Intent(mainA.this, secondA.class);
        for(int i=0;i<numberOfObjects;i++)
            newIntent.putExtra("object"+i,myObjects[i]);
        newIntent.putExtra("length", x);
        startActivity(newIntent);

Second activity:

        Serializable n = getIntent().getSerializableExtra("length");
        int x = Integer.parseInt(n.toString());
        myObjects = new objClass[x];
        for(int i=0;i<x;i++)
            myObjects[i] = (objClass) getIntent().getSerializableExtra("object"+i);

Even if this method works, and gets me the right results, isn't there a better/faster/cleaner solution?(I searched a lot, but haven't found a better way, maybe I don't know what exactly to look for).

relysis
  • 1,065
  • 1
  • 14
  • 27

2 Answers2

1

Make your object serializable and put all objects in a Arraylist and send it.ArrayList itself implements serializable.

 Intent newIntent = new Intent(mainA.this, secondA.class);
 newIntent.putExtra("list",listOfObjets);
 startActivity(newIntent);

Second Activity:

   ArrayList<YourObjects> list =  (ArrayList<YourObjects>)getIntent().getSerializableExtra("list");
Vibhor Bhardwaj
  • 3,071
  • 5
  • 28
  • 49
0

Hi you can try this as well.. Create setter getter class which will set and get your object. next create the array list of your setter getter class.next add all objects into arraylist using set method in for loop. next add that arraylist object into bundle using setArguments. Same you can access that bundle using getArguments that's it.

Mehboob Maniyar
  • 238
  • 1
  • 3
  • 11