Global [application-wide] Variables

Discussion in 'Application Development' started by Sam Voss, Oct 17, 2010.

  1. Sam Voss

    Sam Voss Well-Known Member
    43

    Jun 25, 2010
    367
    44
    43
    Programmer, in school
    Wisconsin
    hey everybody, I have an idea on what I'm going to do with this, the logic behind it i'm pretty sure is solid, but I'm passing it by you all before trying it, for two reasons:
    1, i'm tired so its not getting done tonight, and would like to see the responses to your ideas and
    2, please let me know if you see an obvious fail of a flaw here.

    Alright, so starting off, In java (learned this before Android) when we needed a variable from across an "application", we would have one .java file like this:

    Code (Text):
    1. public class variables{
    2. public int a = 1;
    3. public int b = 2;
    4. public boolean c = true;
    5. public boolean d = false;
    6. public String e = "start";
    7. public String f = "end";
    8. // and so on from here, initializing variables, or actually giving them an //assignment as i did above
    9.  
    10. // from here down, we would create get() and set() methods such as:
    11.  
    12. public boolean getBoolean(String x)//we would use the string to identify which boolean we're getting
    13. {
    14.       if(x.equals("c"))
    15.            return c;
    16.       if(x.equals("d"))
    17.            return d;
    18.       else
    19.            return null;
    20. }
    21.  
    22. //then a set boolean which was same as first, but with a second input var. for what we want the variable to end up as, i'm not going to recreate this
    23. }
    24.  
    so, if I'm going to have lets say a dozen boolean variables in my options menu(such as like check boxes) and on each activity start, just retrieve these, with in the get() method, add something like "all" and returns some kind of them all, probably in an Array/ArrayList, then obvioslly onClose of the activity, just save them by using the set(), then at the end of set(), have a method called called save(), that saves them all. any obvious flaws here? please and thank you!
     

    Advertisement

  2. So, you would like to have a set of variables available to all Activities within application? I guess that's the responsibility of android.app.Application class. However, if you want to have those variables (values) available after application re-start, I guess android.content.SharedPreferences and android.preference.PreferenceManager are the classes you'd want to look at.
     

Share This Page

Loading...