如何在公共类上使用静态变量在活动之间传递数据? [英] How to pass data between activities using static variables on a public class?

查看:126
本文介绍了如何在公共类上使用静态变量在活动之间传递数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to use static variables on a public class for passing them between activities.

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

I'm having a rare problem doing that. I'm giving values to the static variables on a activity. This activity calls a GLSurfaceView and listen for screen orientation changes.

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

If i give values to the static variables on the GLSurfaceView, then, all works fine, the values are stored and i can retrieve them when the onCreate method is called again after a screen orientation change.

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

The problem is when i store values on the static variables outside the GLSurfaceView class, inside the onTouchListener methods of the activity. These values are not stored properly on the static variables, because when i try to access them on the GLSurfaceView those values are not what they were supossed to be.

这是我的静态变量类:

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)    
}

这里我将值存储在我的活动类中(我进行了调试,并且显然它们已正确存储):

Here i'm storing the values on my activity class (i did a debug and aparently they are being stored correctly):

        for (int i=0;i<thumbnailLinearLayouts.size();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,而不是之前存储的值.

And here i'm trying to retrieve these values on the GLSurfaceView class, and the values are not correct, It is retrieving the original initial value, 1, instead of the value stored before.

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

If Android kills and restarts the process for your application, then the static variables will get assigned to their default values. You might be better of using SharedPreferences instead of static variables: http://developer.android.com/guide/topics/data/data-storage.html#pref

这篇关于如何在公共类上使用静态变量在活动之间传递数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆