1. 通过intent来传递:
activity1中设置
:
A.传字符等:
Java代码
String text = "hello";
Intent intent1 = new Intent(ActivityMain.this, Activity2.class);
intent1.putExtra("activity1", text);
startActivity(intent1);
B. 传对象,对象要实例化,继承Serializable
Java代码
Bundle mbundle=new Bundle();
mbundle.putSerializable("user",userList.get(position));
Intent in =new Intent (getApplicationContext(), activity2.class);
in.putExtras(mbundle);
startActivity(in);
activity2中接收:
A:接收
Java代码
Bundle extras = getIntent().getExtras();
if (extras != null) {
textview.setText(extras.getString("activity1"));
}
B.接收
Java代码
Bundle bundel = getIntent().getExtras();
user= (User) bundel.get("user");
2. SharedPreferences
在activity1中设置的如下:
Java代码
SharedPreferences sp = getSharedPreferences("textinfo",0);
Editor editor = sp.edit();
String text = "hello";
editor.putString("text", text);
editor.commit();
Intent i = new Intent(getApplicationContext(),activity2.class);
startActivity(i);
跳转到Message的activity,获取内容如下
Java代码
SharedPreferences share = getSharedPreferences("textinfo",0);
String text = share.getString("text", null);
msgtextview.setText(text);
Android中2个activity之间的数据传递方法
转载本文章为转载内容,我们尊重原作者对文章享有的著作权。如有内容错误或侵权问题,欢迎原作者联系我们进行内容更正或删除文章。
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
Android面试题之activity启动流程
Android面试题之activity启动流程总结
加载 初始化 Android 面试题 activity -
Activity之间的数据传递
原理图:1)较少数据时存:intent.putExtra("username", username);取:String
android bundle xml