How to create a global variable in android? [duplicate]

落花浮王杯 提交于 2019-12-18 10:44:54

问题


In my android application I need a place for putting the variable member id . The problem is , it is getting from the online API and I need to find a way to store / retrieve it

I have tried to put it in a custom class , but the problem is , it will lose if I kill the activity, I have also know that there is a way to extends the application.

So I would like to know what is the best way to store global variable?

I have to implment:

  1. Save the variable on onSaveState
  2. Save it on sharepref
  3. Save it manually
  4. Retrieve it manually

Thanks

Update: Thanks for reply. If I have just 3 variable (simple data e.g. a boolean , a phrase ), and I need it after app restart , should I simply use share pref to store it? What is the drawback of it? e.g. Will it harmful to the performance? thanks


回答1:


You can create the global variable in android by declaring them in the class which extend the Application class.

Something like this.

class MyAppApplication extends Application {

    private String mGlobalVarValue;

    public String getGlobalVarValue() {
        return mGlobalVarValue;
    }

    public void setGlobalVarValue(String str) {
        mGlobalVarValue = str;
    }
}

MainActivity.java

class MainActivity extends Activity {
    @Override
    public void onCreate(Bundle b){
        ...
        MyAppApplication mApp = ((MyAppApplication)getApplicationContext());
        String globalVarValue = mApp.getGlobalVarValue();
        ...
    }
}

Update

This hold the value until your application is not destroyed. If you want to keep your values save even after your application instance destroy then you can use the SharedPreferences best way to do this.

Learn about the SharedPrefernces from here : http://developer.android.com/reference/android/content/SharedPreferences.html




回答2:


  • If you need a global variable, which is shared between multiple activities and survives their lifecycle changes, but is otherwise not persisted and would not survive an application restart, the method suggested in the first answer (extending Application and putting it there) is ok.
  • If you need a persisted global setting, which should also survive an application restart, then you should use one of the other methods suggested. SharedPreferences is probably the easiest to use.

See also this Stack Overflow question: What's the best way to share data between activities?




回答3:


You can store it in a SharedPreferences, and create a sharedpreferenceHelper class to retrieve/store it.




回答4:


Its always better to store values which you want to use multiple times in one of the following ways:-

  • Shared Preferences
  • Singleton classes
  • Write it to a file
  • SQLite Database
  • Store on the web-server


来源:https://stackoverflow.com/questions/21810240/how-to-create-a-global-variable-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!