-1

I'm building an application in which I create the same ArrayList in every Activity. An other option is creating the ArrayList in the first Activity and then pass it on to other activities. However, I don't know if this is 'better' than creating the same ArrayList every time. I know by creating a new ArrayList it would take up a bit of memory (ArrayList is not so big), but passing an ArrayList by using an Intent slows your app down, right?

What's the best option to go with?

With kind regards, Koen

  • passing array list. You will not be passing actual arraylist, it will be the reference always. – Pirate Jun 14 '21 at 10:16
  • `but passing an ArrayList by using an Intent slows your app down, right?` if this were the case, why would google still be allowing it to be done ? if it was that bad, wouldn't it have been deprecated by now ? you're trying to do premature optimization here, realistically, none of this will make a difference. choose what makes the most sense for you – a_local_nobody Jun 14 '21 at 10:18
  • `it will be the reference always` No. @Pirate. First the list is copied to the intent. Then at receiving side the list is extracted from the intent. There are then three complete lists in memory. – blackapps Jun 14 '21 at 10:57
  • i commented with respect to java lists. @bla – Pirate Jun 14 '21 at 11:05

3 Answers3

1

You can simply make an ArrayList in custom Application class and use it in every Activity

Masoud Badrmiran
  • 421
  • 2
  • 4
  • 11
0

Why don't you try to provide the Arraylist system wide?

For example this could be an option: Android global variable

profbreach
  • 16
  • 4
0

Arraylist in java are the reference variable so passing an arrayList is better approach than creating every time, unless you don't have any write operation on it, otherwise it will give you inconsistency result,