1

In an Android Activity I'm filling an ArrayList which is declared as static in that activity. This data structure is accessed later by other activities during application execution as follows:

ClassName.data_structure_name;

My question is if it's safe to do it this way or if Android can clear the structure of memory or something like that?

notGeek
  • 1,394
  • 5
  • 21
  • 40

4 Answers4

1

Yes, Android can dispose of your Activity as explained here.

If an activity is paused or stopped, the system can drop it from memory either by asking it to finish (calling its finish() method), or simply killing its process. When the activity is opened again (after being finished or killed), it must be created all over

But this is for Activity instances. If your ArrayList is static then it won't be dropped unless the Activity class is unloaded from the Dalvik VM (which personally I've never seen happening).

Anyway, IMHO it's not a good design (I've used it in the past). I suggest you use a separate class to store your static ArrayList. Just beware of multi-threading and the cons of using Singletons.

This and this SO questions are also worth reading.

Community
  • 1
  • 1
m0skit0
  • 25,268
  • 11
  • 79
  • 127
1

If you need your ArrayList to be accessed by different activities, I think you should put it in a separate class - not an Activity one.

Olivier Picault
  • 338
  • 5
  • 14
0

The data won't persist across different processes, if that's what you're wondering, so you'll probably just get errors. IE, static members of classes are not persistent storage.

However, if you these later activities are within the same process, then you'll be able to access it like you've specified.

GraphicsMuncher
  • 4,583
  • 4
  • 35
  • 50
0

couple of issues with this design .

  1. Breaking rules of Software Engineering and OOP
  2. You are also breaking rules of MVC design pattern
  3. When your activity is not needed it will occupy memory

I guess you getting data from some source and putting it into Array List. if you do not need entire array list just pass the value as inside a bundle.

if you need entire array list in subsequent activities repopulate array list the way you get at first place.

Static Access do not go well with OOP design. Except following case for printing log tag i will not use static anywhere in my Android app.

public static final String TAG="MY_TAG";

Even Singleton is highly debatable.

minhaz
  • 4,233
  • 3
  • 33
  • 49
  • I need the entire array. Are you suggesting to make a new request to web server and the repopule the array?! More data traffic, more waiting time for the users.. does not seem a good idea, right? – notGeek Mar 25 '13 at 01:31
  • if you are getting it from web source, then cache it internally using persistence storage with a time. Every time you read data compare with stored time. if it is too old fetch the data from web server again and update time and data. You are suppose to use non UI thread to network operation. Make sure every time you make a request to web server old request is not pending. For example if your request is onCreate every time user rotate screen you will end-up making new request. – minhaz Mar 25 '13 at 01:43
  • *"When your activity is not needed it will occupy memory"* - false, this is a **static** member. And you forgot `final` in your `TAG` declaration ;) – m0skit0 Mar 25 '13 at 02:25
  • Point 3 is saying what you explained in your answer. – minhaz Mar 25 '13 at 02:42
  • Not really. This is a static member (a class member), has nothing to do with `Activity` **instances**. All of his `Activity` instances can be disposed of and the field value will be maintained. – m0skit0 Apr 02 '13 at 08:54