0

I've created some getters setters in my Application class as so:

public class myApp extends Application{

//"Global" variables

private Boolean musicEnabled = true;        //Music on or off?
private Boolean soundEnabled = true;        //Sound effects on or off?

@Override
public void onCreate() {
    // TODO Auto-generated method stub

    musicEnabled = true;            //Default value for music
    soundEnabled = true;            //Default value for sound effects

    super.onCreate();
}

//Getter and setter for musicEnabled

public Boolean getMusicOption(){
    return musicEnabled;                    //Getter


}
public void setMusicOption(Boolean value){  //Setter

    musicEnabled = value;

    }

//Getter and setter for soundEnabled

public Boolean getSoundOption(){            
    return soundEnabled;
}

public void setMusicOptions(Boolean value){
    soundEnabled = value;             
}

}

I then get the values in my Activity class as so:

myApp myAppSettings = (myApp)getApplicationContext();   
musicEnabled = myAppSettings.getMusicOption();
soundEnabled = myAppSettings.getSoundOption();

This is fine but what I can't figure out is how I can get to them and use them from my corresponding surfaceView class? i.e. the class that starts:

public class mySView extends SurfaceView implements
  SurfaceHolder.Callback {

The only way I've manages to do this so far is pass them into my surfaceview class by creating a method like:

public void initialise(Boolean Sound, Boolean Music){

}

And then passing these in from my Activity class like so:

myView.initialise(musicEnabled, soundEnabled).

This works, however it seems a bit messy, I mean I am going to need to use the setters from my 'myView' class to set these value so........ is there anyway I can access them directly from my 'myView' class or do I have to do this from the Activity class?

Thanks all

Zippy
  • 3,826
  • 5
  • 43
  • 96
  • In my opinion, passing in arguments to constructors and class methods is a lot less messy than your approach. Ease of maintenance, extensibility and reuse are much better when not relying on spaghetti code to access random global variables. – Simon Feb 27 '13 at 19:27
  • Since they are simple booleans, why not just use SharedPreferences rather than a whole Application class? – daniel_c05 Feb 27 '13 at 19:28
  • @Simon, just so I'm clear, are you saying to just pass the variable / value in from my application class to my surfaceview class like I am already doing (but do away with the application class)? Thanks. – Zippy Feb 27 '13 at 22:59
  • @daniel_c05, from what I understand even if I used SharedPreferences I would still need the application class to access said SharedPreferences from a view/surfaceview class? – Zippy Feb 27 '13 at 23:19
  • Every View has a/the context which can be used to resolve the SharedPreferences. You can't create anything visible without having any kind of context, is a fundamental part of any App from which resources and more are resolved. You should take some time and read the documentation. – Nicklas Gnejs Eriksson Feb 28 '13 at 17:37
  • I have read the documentation - many times but it's not the easiest to understand unfortunately. I actually saw someone else post the same question (about getting to sharedPrefs through a view class) and they were told to create an application class. :-) – Zippy Feb 28 '13 at 19:45

3 Answers3

1

You should just be able to call getContext().getApplicationContext() from inside your custom SurfaceView, and typecast it like you would in your above example. See View.getContext().

wsanville
  • 37,158
  • 8
  • 76
  • 101
1

You can create some method, e.g. init() and call it from all of constructors of your mySView.

In init method you can do the same, as in the Activity:

private void init() {
    myApp myAppSettings = (myApp)getContext().getApplicationContext();   
    musicEnabled = myAppSettings.getMusicOption();
    soundEnabled = myAppSettings.getSoundOption();
}
Anton-M
  • 1,113
  • 7
  • 13
0

Another option would be to save your settings in the SharedPreferences which is accessable anywhere in the App where you have the Context. This way any changes from the last use of your App will automatically be persisted as can be loaded during next launch without having to return the values to default every launch.

SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
Nicklas Gnejs Eriksson
  • 3,395
  • 2
  • 21
  • 19