Java 如何在android中创建全局变量?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21810240/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 10:47:26  来源:igfitidea点击:

How to create a global variable in android?

javaandroidclassvariablesglobal-variables

提问by user782104

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

在我的 android 应用程序中,我需要一个放置变量成员 id 的地方。问题是,它是从在线 API 获取的,我需要找到一种方法来存储/检索它

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
  1. 在 onSaveState 上保存变量
  2. 将其保存在 sharepref 上
  3. 手动保存
  4. 手动检索

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

更新:谢谢回复。如果我只有 3 个变量(简单数据,例如一个 boolean ,一个短语),并且在应用程序重新启动后我需要它,我是否应该简单地使用 share pref 来存储它?它的缺点是什么?例如,它会不会对性能有害?谢谢

采纳答案by Ajay S

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

您可以通过在扩展Application类的类中声明它们来在 android 中创建全局变量。

Something like this.

像这样的东西。

class MyAppApplication extends Application {

    private String mGlobalVarValue;

    public String getGlobalVarValue() {
        return mGlobalVarValue;
    }

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

MainActivity.java

主活动.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 SharedPreferencesbest way to do this.

这将保留该值,直到您的应用程序没有被销毁。如果您想在应用程序实例销毁后仍保存您的值,那么您可以使用SharedPreferences最好的方法来做到这一点。

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

SharedPrefernces从这里了解:http: //developer.android.com/reference/android/content/SharedPreferences.html

回答by StarsSky

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

您可以将它存储在SharedPreferences 中,并创建一个 sharedpreferenceHelper 类来检索/存储它。

回答by Atish Agrawal

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
  • 共享偏好
  • 单例类
  • 将其写入文件
  • SQLite 数据库
  • 存储在网络服务器上

回答by stoyanr

  • 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 Applicationand 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. SharedPreferencesis probably the easiest to use.
  • 如果您需要一个全局变量,该变量在多个 Activity 之间共享并在其生命周期更改后继续存在,但不会持久化并且不会在应用程序重启后继续存在,则第一个答案中建议的方法(Application将其扩展并放在那里)是可以的。
  • 如果您需要一个持久的全局设置,它也应该在应用程序重新启动后继续存在,那么您应该使用建议的其他方法之一。SharedPreferences可能是最容易使用的。

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

另请参阅此堆栈溢出问题:在活动之间共享数据的最佳方式是什么?