0

Is it a good idea to pass a list of object from one Activity to another for Android ?

It is quiet troublesome to pass a list of object in an intent, and I wonder whether it affect the performance if the object list is too large or the object is too complicated.

Is it a better solution to get the list of object from other place, for example, query the DB once more , or save the list of object in a temporary class and fetch it in new Activity?

Zhli
  • 380
  • 1
  • 10
  • 1
    Intents are used to send primitive datatypes to other Activity. Its better to fetch list of objects in Other Activity rather them passing. – kevz Feb 16 '16 at 11:03
  • Have your objects implement parcelable, then you can pass them through bundle/extra OR best idea is to use a database – An-droid Feb 16 '16 at 11:04
  • Cache, using for example Gson; Read from DataBase (SQLite); Use public static List in Activity A – Volodymyr Kulyk Feb 16 '16 at 11:13

2 Answers2

1

As long as you are passing Parcelable objects' list, nothing's wrong when passing through Intent. Regarding performance, that is up to you and the amount of data you pass.

As per my experience, If you are passing data up to 1MB of size, it should be fine. Anything above that will fail as this seems to be the limit. Read this.

Besides, you are welcome to use preferences, SQLite, files or static-object-referencing methodologies to fetch your data anytime and anywhere.

Community
  • 1
  • 1
waqaslam
  • 67,549
  • 16
  • 165
  • 178
0

Solution1 : Use Intent

Send :

     Intent data = new Intent(Activity1.this,Activity2.class);
    data.putParcelableArrayListExtra(Constant.LIST_OBJECT, 
              (ArrayList<?    extends Parcelable>) getObjects());

receive :

      List<YOUR_OBJECT> objects = data.getParcelableArrayListExtra(Constant.LIST_OBJECT);

Solution2 :

public class SessionData {

  private static SessionData instance;
  private final List< YOUR_OBJECT > listSessionObjects; 

  private SessionData() {
     listSessionObjects = new ArrayList<>();
  }

  public static final SessionData getInstance() {
    if (instance == null) {
          instance = new SessionData();
    }
    return instance;
  }

   public List<YOUR_OBJECT> getListSessionObjects() {
      return listSessionObjects;
  }

  public void setListSessionObjects(List<YOUR_OBJECT > objects) {
     listSessionObjects = objects
  }

 }

to use it :

 SessionData.getInstance().getListSessionObjects();
 SessionData.getInstance(). setListSessionObjects(objects);
MrZ
  • 560
  • 2
  • 12