Global variables in android app


Last Updated:

  1. eljainc

    eljainc Member This Topic's Starter

    Joined:
    Oct 28, 2010
    Messages:
    6
    Likes Received:
    0
    Hello,

    I'm trying to implement a global property to variables. From another activity, I'd like to access values that are in another activity. From what I've seen through Googling, it seems that adding a class like the following will resolve this issue:

    class MyApp extends Application {

    private String myState;

    public String getState(){
    return myState;
    }
    public void setState(String s){
    myState = s;
    }
    }

    class Blah extends Activity {

    @Override
    public void onCreate(Bundle b){
    ...
    MyApp appState = ((MyApp)getApplicationContext());
    String state = appState.getState();
    ...
    }
    }

    After doing this, I'm getting application errors immediately when the application starts execution on the emulator. What would cause this behavior?

    Is there a better way to have global variables in use throughout an android application?

    Thanks
    Mike
     

    Advertisement
  2. mblenton

    mblenton Member

    Joined:
    Nov 6, 2010
    Messages:
    6
    Likes Received:
    0
    Did you register the Application in the AndroidManifest.xml?

    <application android:label="@string/app_name"
    android:name="YourApplication">

    etc...
     

Share This Page

Loading...