How to declare global variables in Android?

前端 未结 17 2940
死守一世寂寞
死守一世寂寞 2020-11-21 04:12

I am creating an application which requires login. I created the main and the login activity.

In the main activity onCreate method I added the following

17条回答
  •  温柔的废话
    2020-11-21 05:09

    class GlobaleVariableDemo extends Application {
    
        private String myGlobalState;
    
        public String getGlobalState(){
         return myGlobalState;
        }
        public void setGlobalState(String s){
         myGlobalState = s;
        }
    }
    
    class Demo extends Activity {
    
    @Override
    public void onCreate(Bundle b){
        ...
        GlobaleVariableDemo appState = ((GlobaleVariableDemo)getApplicationContext());
        String state = appState.getGlobalState();
        ...
        }
    }
    

提交回复
热议问题