`
wyhlzxl
  • 浏览: 33911 次
  • 性别: Icon_minigender_1
  • 来自: 合肥
社区版块
存档分类

2个activity 之间的数据传递

阅读更多
初学android,小小的学习总结

1.通过intent来传递:
A.传字符等:activity1中设置:
String text = "hello";
	Intent intent1 = new Intent(ActivityMain.this, Activity2.class);
	intent1.putExtra("activity1", text);
	startActivity(intent1 );


B.传对象,对象要实例化,继承Serializable
Bundle mbundle=new Bundle();			mbundle.putSerializable("user",userList.get(position));
Intent in =new Intent (getApplicationContext(), activity2.class);
in.putExtras(mbundle);
startActivity(in);



activity2中接收:
        
A:接收
Bundle extras = getIntent().getExtras();
	        if (extras != null) {
	            textview.setText(extras.getString("activity1"));
	        }


B.接收 
 Bundle bundel = getIntent().getExtras();
         user= (User) bundel.get("user"); 



2.SharedPreferences

我在activity1中设置的如下:
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,获取内容如下
SharedPreferences share=getSharedPreferences("textinfo",0);
		String text =share.getString("text", null);
		msgtextview.setText(text);

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics