Reputation: 1243
So i have made a class DrawView which extends View and i want to draw some graph in that class with the points i stored as int array i made this array like a sort of public variable with the help of How to declare global variables So when i want to get connected with MyApp in Activity and change my array, i simply use
MyApp appState = ((MyApp)getApplicationContext());
but the problem is that this won't work when i call it in my DrawView.java class. Any ides how to solve this?
Upvotes: 1
Views: 1575
Reputation: 39856
I really don't know why that answer is so up voted as it's not a good solution. The Application object is to run the Application, not to store data, you can solve this MUCH easier with a simple Singleton object, try this:
public Class MyData{
private int[] data;
private static MyData me;
public int[] getData(){
return data;
}
private MyData(){} // private constructor
public MyData get() {}
if(me==null) me = new MyData();
return me;
}
}
than from any object you can call:
int[] data = MyData.get().getData()
and feel free to expand to more than just a int[] ... put any other object that you want to be globally accessible. But remember, DO NOT KEEP REFERENCES TO THE CONTEXT!
Upvotes: 1