How to declare global variables in Android?


Sometimes you have need define or declare most of the variable globally access, in such senario you have define or declare global variable like this which is simple, sooniln define on stackoverflow very simple way:

class MyApp extends Application {

  private String myState;

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

Now to you have to access above defined variable before you have setStatus.

class Blah extends Activity {

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


Happy Coding!!!


How to declare global variables in Android? How to declare global variables in Android? Reviewed by D Kshetri on 12:24 AM Rating: 5

No comments:

Powered by Blogger.