android静态变量传递数据,android – 如何在公共类上使用静态变量在活动之间传递数据?...

我试图在公共类上使用静态变量来在活动之间传递它们.

我这样做有一个罕见的问题.我给活动上的静态变量赋值.此活动调用GLSurfaceView并侦听屏幕方向更改.

如果我给GLSurfaceView上的静态变量赋值,那么,一切正常,值存储,并且我可以在屏幕方向更改后再次调用onCreate方法时检索它们.

问题是当我将值存储在GLSurfaceView类之外的静态变量上时,在活动的onTouchListener方法中.这些值没有正确存储在静态变量上,因为当我尝试在GLSurfaceView上访问它们时,这些值并不是它们所支持的值.

这是我的静态变量类:

public class MagazineStatus {

//clase utilizada para almacenar variables estáticas.

static int currentPage=1; //página actual

//Valores originales cuando pasamos de un modo a otro, por ejemplo, de portrait a landscape.

static float oScale=0.0f;

static float oX=0.0f;

static float oY=0.0f;

static float oZrot=0;

static boolean modeChanged=false; //indica si hemos cambiado de modo

(landscape/portrait)

}

这里我将值存储在我的活动类中(我做了一个调试,显然它们正确存储):

for (int i=0;i

final int auxIndex=i;

thumbnailLinearLayouts.get(i).setOnTouchListener(new OnTouchListener(){

@Override

public boolean onTouch(View v, MotionEvent event) {

MagazineStatus.currentPage=auxIndex;

System.out.println("MagazineStatus.currentPage: "+MagazineStatus.currentPage);

return true;

}

});

}

在这里,我试图在GLSurfaceView类上检索这些值,并且值不正确,它正在检索原始初始值1,而不是之前存储的值.

currentPage=MagazineStatus.currentPage; //cargo datos guardados antes del cambio de orientación

我做错了什么?

解决方法:

如果Android杀死并重新启动应用程序的进程,则静态变量将被分配为其默认值.您可能更好地使用SharedPreferences而不是静态变量:http://developer.android.com/guide/topics/data/data-storage.html#pref

标签:android,static,android-activity,glsurfaceview

来源: https://codeday.me/bug/20190723/1513576.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值