我试图在公共类上使用静态变量来在活动之间传递它们.
我这样做有一个罕见的问题.我给活动上的静态变量赋值.此活动调用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