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


当前回答

你可以扩展基本的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();

其他回答

试着这样做:

创建共享数据类:

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();

如果可能的话,你应该在文件中声明你需要保持活跃的变量,这些变量没有被垃圾收集器清除或被OS卸载 要做到这一点,你必须用C/ c++编写代码,并编译为.so lib文件,并将其加载到MainActivity中。

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

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

你可以扩展基本的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();
// 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;