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

谁说胖子不能爱 提交于 2019-12-06 00:33:40

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

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;
venu

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

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