2014-06-27 59 views
-2

我有很多活动和MainActivity,并有两个按钮(B1-B2)。在B1(活性1)将用户写同样的数据和完成后回到MainActivity 和B2(活性2)将得到(活性1)如何将活动中的任何数据发送到其他活动?

(活性1)

Intent i = new Intent(getApplicationContext(), Activity2.class); 
    i.putExtra("new_variable_name","value"); 

(活性2)所有的日期

Bundle extras = getIntent().getExtras(); 
if (extras != null) { 
    String value = extras.getString("new_variable_name"); 


} 

此代码不适用于我!! ....任何解决!

+0

http://stackoverflow.com/questions/4967740/transfer-data-from-one-activity-to -otherother-activity-using-intents http://stackoverflow.com/questions/18146614/how-to-send-string-from-one-activity-to-another –

回答

0

从SO帖子:

Intent i =new Intent(Info.this, GraphDiag.class).putExtra("new_variable_name", "value"); 
startActivity(i); 

获取数据

String value = getIntent().getStringExtra(<StringName>); 

Also look here

0

你的代码是正确的我猜的背景下getApplicationContext()引起的问题。在活动范围内,最好使用MyActivity.this作为上下文参数。试试这个:

Intent i =new Intent(Activity1.this, Acivity2.class); 
i.putExtra("new_variable_name", "value"); 
startActivity(i); 

为了获得额外的价值,并执行空校验:

Bundle b = getIntent().getExtras(); 
if(b!=null){ 
String value = b.getString("new_variable_name");//ensured that the string is not null 
} 
+0

但如何知道它是空的或不是? – BuGsHaN

+0

它与这个laine startActivity(i)一起工作;我不需要startActivity ...如果用户返回MainActivity之后,转到Acivity2并将获得ListView或Toast中的所有数据? – BuGsHaN

+0

@ user3784315检查我编辑的答案 –

相关问题