1

In my mainActivity I declare static List like this:

public static List<Map<String, String>> ArrList = new ArrayList<Map<String, String>>();

Then in other activities I access this list with mainActivity.ArrList ... I am almost sure that I am not supposed to do this, but anyway, I would like to understand all of it a little bit better...

So, here are my questions: 1) As far as I know, due to low memory or something, my mainActivity could get destroyed (while another activity is in focus) and in that case my ArrList would get destroyed too. Is that right? 2) Whene I exit, then restart, my app crashes and debugger reports OutOfMemory. I assume upon restart android allocates another block of memory for my static List. So, I also assume that I should destroy static List when Activity ends. How to do that properly?

I appreciate any other advice about this matter.

1

2 Answers 2

1
  1. You can use static variables - there are no limitations on that (except highely undesirable to keep in static variables Activity/Context objects). Also some people dislike to use static ones saying it's not very "stylish"

  2. If you're going to destroy static list upon destroying Activity - that means you don't need static list, huh? Static variables are usefull when you need something common across all instances of given class.

  3. Best place to store global variables is to extend Application class declare there private members accessible through getters/setters, like:

    public class MyApp extends Application
    {
        private List<Map<String, String>> ArrList;
    //...
    }
    

    In this case you'd need to declare MyApp in android manifest (look here)

Sign up to request clarification or add additional context in comments.

Comments

0
  1. your static ArrayList will be cleared of its values when your process is killed(resulting in an empty list, but still valid and not null). you will need to reload the list after you resume from being killed

  2. this static array may retain it's values even after you "quit" your application(but not after its process is killed), so make sure you clear the array before you quit, so it's not filled up again(adding double+ the values). ArrList.clear();

using global static lists like this can turn into a headache when you start coding your app to be multitasking-friendly(able to recover fully from being killed). any activity which uses this list must be able to resume itself and re-populate the list if needed.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.