How to pass data between activities using static variables on a public class?

久未见 提交于 2020-01-02 07:18:49

问题


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

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.

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.

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.

This is my static variables class:

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

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

What i am doing wrong?


回答1:


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




回答2:


define variable as public static and then use it throughout entire application,

eg

public static String xyz = "abcd";

now in any class to use 'xyz' just use it as under

classname.xyz;



回答3:


Second Activity :-

In xml File take one textview field

Java Code

public static String name;

TextView t=(TextView)findViewById(R.id.tv);

t.setText(name);

First Activity:-

in xml file take one edittext and one button

Java Code

button.setOnClickListener(new View.onClickListener){

    @override
    public void onClick(View v){

    SecondActivity.name=editText.getText().toString();

    Intent i=new Intent(firstActivity.this,SecondActivity.class);

    startActivity(i);

    }
}


来源:https://stackoverflow.com/questions/9637761/how-to-pass-data-between-activities-using-static-variables-on-a-public-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!