如何在应用程序的生命周期中创建全局变量,而不管哪个活动正在运行。
当前回答
试着这样做:
创建共享数据类:
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();
其他回答
这个全局变量适用于我的项目:
public class Global {
public static int ivar1, ivar2;
public static String svar1, svar2;
public static int[] myarray1 = new int[10];
}
// How to use other or many activity
Global.ivar1 = 10;
int i = Global.ivar1;
试着这样做:
创建共享数据类:
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();
简单! !
那些你想作为全局变量访问的变量,你可以声明为静态变量。现在,你可以通过
classname.variablename;
public class MyProperties {
private static MyProperties mInstance= null;
static int someValueIWantToKeep;
protected MyProperties(){}
public static synchronized MyProperties getInstance(){
if(null == mInstance){
mInstance = new MyProperties();
}
return mInstance;
}
}
MyProperites.someValueIWantToKeep;
Thats It !;)
如果可能的话,你应该在文件中声明你需要保持活跃的变量,这些变量没有被垃圾收集器清除或被OS卸载 要做到这一点,你必须用C/ c++编写代码,并编译为.so lib文件,并将其加载到MainActivity中。
import android.app.Application;
public class Globals extends Application
{
private static Globals instance = null;
private static int RecentCompaignID;
private static int EmailClick;
private static String LoginPassword;
static String loginMemberID;
private static String CompaignName = "";
private static int listget=0;
//MailingDetails
private static String FromEmailadd="";
private static String FromName="";
private static String ReplyEmailAdd="";
private static String CompaignSubject="";
private static int TempId=0;
private static int ListIds=0;
private static String HTMLContent="";
@Override
public void onCreate()
{
super.onCreate();
instance = this;
}
public static Globals getInstance()
{
return instance;
}
public void setRecentCompaignID(int objRecentCompaignID)
{
RecentCompaignID = objRecentCompaignID;
}
public int getRecentCompaignID()
{
return RecentCompaignID;
}
public void setLoginMemberID(String objloginMemberID)
{
loginMemberID = objloginMemberID;
}
public String getLoginMemberID()
{
return loginMemberID;
}
public void setLoginMemberPassword(String objLoginPassword)
{
LoginPassword = objLoginPassword;
}
public String getLoginMemberPassword()
{
return LoginPassword;
}
public void setEmailclick(int id)
{
EmailClick = id;
}
public int getEmailClick()
{
return EmailClick;
}
public void setCompaignName(String objCompaignName)
{
CompaignName=objCompaignName;
}
public String getCompaignName()
{
return CompaignName;
}
public void setlistgetvalue(int objlistget)
{
listget=objlistget;
}
public int getlistvalue()
{
return listget;
}
public void setCompaignSubject(String objCompaignSubject)
{
CompaignSubject=objCompaignSubject;
}
public String getCompaignSubject()
{
return CompaignSubject;
}
public void setHTMLContent(String objHTMLContent)
{
HTMLContent=objHTMLContent;
}
public String getHTMLContent()
{
return HTMLContent;
}
public void setListIds(int objListIds)
{
ListIds=objListIds;
}
public int getListIds()
{
return ListIds;
}
public void setReplyEmailAdd(String objReplyEmailAdd)
{
ReplyEmailAdd=objReplyEmailAdd;
}
public String getReplyEmailAdd()
{
return ReplyEmailAdd;
}
public void setFromName(String objFromName)
{
FromName=objFromName;
}
public String getFromName()
{
return FromName;
}
public void setFromEmailadd(String objFromEmailadd)
{
FromEmailadd=objFromEmailadd;
}
public String getFromEmailadd()
{
return FromEmailadd;
}
}
推荐文章
- 安卓视图模型与视图模型
- Android的clipToPadding属性做什么?
- 导航抽屉:如何在启动时设置所选项目?
- 在XML中“图像上缺少contentDescription属性”
- 在Android SQLite中处理日期的最佳方法
- 读取Android APK的包名
- Android-Facebook应用程序的键散列
- 登出时,清除活动历史堆栈,防止“返回”按钮打开已登录的活动
- 如何改变标题的活动在安卓?
- 如何隐藏动作栏之前的活动被创建,然后再显示它?
- 是否有一种方法以编程方式滚动滚动视图到特定的编辑文本?
- 在Android中将字符串转换为Uri
- 如何在NestedScrollView内使用RecyclerView ?
- 移动到另一个EditText时,软键盘下一步点击Android
- Android应用中的GridView VS GridLayout