如何在应用程序的生命周期中创建全局变量,而不管哪个活动正在运行。


当前回答

您可以使用应用程序首选项。只要您传递Context对象,就可以从任何活动或代码段访问它们,并且它们对于使用它们的应用程序是私有的,因此您不需要担心暴露应用程序特定的值,除非您处理路由设备。即使如此,您也可以使用哈希或加密方案来保存值。此外,这些首选项将从一个应用程序运行到下一个应用程序运行。 下面是您可以参考的一些代码示例。

其他回答

你可以扩展基本的android.app.Application类,并像这样添加成员变量:

public class MyApplication extends Application {

    private String someVariable;

    public String getSomeVariable() {
        return someVariable;
    }

    public void setSomeVariable(String someVariable) {
        this.someVariable = someVariable;
    }
}

在你的android manifest中,你必须声明实现android.app. application的类(添加android:name="。属性到现有的应用程序标签):

<application 
  android:name=".MyApplication" 
  android:icon="@drawable/icon" 
  android:label="@string/app_name">

然后在你的活动中,你可以像这样获取和设置变量:

// set
((MyApplication) this.getApplication()).setSomeVariable("foo");

// get
String s = ((MyApplication) this.getApplication()).getSomeVariable();

Technically this does not answer the question, but I would recommend using the Room database instead of any global variable. https://developer.android.com/topic/libraries/architecture/room.html Even if you are 'only' needing to store a global variable and it's no big deal and what not, but using the Room database is the most elegant, native and well supported way of keeping values around the life cycle of the activity. It will help to prevent many issues, especially integrity of data. I understand that database and global variable are different but please use Room for the sake of code maintenance, app stability and data integrity.

你可以像这样使用单例模式:

package com.ramps;

public class MyProperties {
private static MyProperties mInstance= null;

public int someValueIWantToKeep;

protected MyProperties(){}

public static synchronized MyProperties getInstance() {
        if(null == mInstance){
            mInstance = new MyProperties();
        }
        return mInstance;
    }
}

在你的应用程序中,你可以这样访问你的单例:

MyProperties.getInstance().someValueIWantToKeep

试着这样做:

创建共享数据类:

SharedData.java

import android.app.Application;

/**
 * Created by kundan on 6/23/2015.
 */
public class Globals {


    private static Globals instance = new Globals();

    // Getter-Setters
    public static Globals getInstance() {
        return instance;
    }

    public static void setInstance(Globals instance) {
        Globals.instance = instance;
    }

    private String notification_index;


    private Globals() {

    }


    public String getValue() {
        return notification_index;
    }


    public void setValue(String notification_index) {
        this.notification_index = notification_index;
    }



}

在那些你想要设置/获取数据的类中声明/初始化一个类的全局实例(在onCreate()方法之前使用此代码)

Globals sharedData = Globals.getInstance();

组数据:

sharedData.setValue("kundan");

得到的数据:

String n = sharedData.getValue();
// My Class Global Variables  Save File Global.Java
public class Global {
    public static int myVi; 
    public static String myVs;
}

// How to used on many Activity

Global.myVi = 12;
Global.myVs = "my number";
........
........
........
int i;
int s;
i = Global.myVi;
s = Global.myVs + " is " + Global.myVi;