Note that there are some explanatory texts on larger screens.

plurals
  1. POSolution to access global data from everywhere - efficiently
    text
    copied!<p>I need to find a solution that holds and accesses large chunks of complex global data and methods. It has to be accessible from within activities and normal instance variables of various data classes.</p> <p>This is how I have done it. I would just like to know if there is anything wrong with it or if there is a better/cleaner way.</p> <p>First I extend <code>Application</code> like recommended many times...</p> <pre><code>public class MainDataManager extends Application{ public ... large chunks of data in arrays, lists, sets,.... //static variable for singleton access from within instance variables of other classes public static MainDataManager mainDataManager; //create and init the global data, and store it in the static variable of the class @Override public void onCreate() { super.onCreate(); //in case it should get called more than once for any reason if (mainDataManager == null) { init(); mainDataManager = this; } } </code></pre> <p>Now accessing it from within activities like everywhere recommended...</p> <pre><code>MainDataManager mainDataManager = (MainDataManager)getApplicationContext(); </code></pre> <p>And since I need to access it from normal instances of data classes ...</p> <pre><code>public class MyDataClass { public MainDataManager mainDataManager; public String name; public MyDataClass(String namex) { this.name = namex; //this is why I defined the static variable within MainDataManager, so //one has access to it from within the instance of MyDataClass this.mainDataManager = MainDataManager.mainDataManager; } public void examplesForAccessing() { //some examples on how to access the global data structure and associated methods mainDataManager.someMethodAccess(); xyz = mainDataManager.someDataAccess; mainDataManager.someIndirectMethodAccess.clear(); mainDataManager.someOtherData = false; } } </code></pre> <p>Since I have not done this so far, I would like to know if there is anything wrong with this. Memory, efficiency, ...</p> <p>Thanks very much!</p> <p>May I add a little sidenote? I could also have just used a class <code>MainDataClass</code> and access by <code>MainDataClass.var or MainDataClass.method().</code> Is there any REAL disadvantage? </p> <p>Is the data in both cases held in heap/stack?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload