i have two activities main Activity and activity two ,
basically i am trying to pass a text from activity two to main activity when i click the button in main activity
but its always retrieve null
any help why it doesn't send back the message
that is main activity code :
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button two = (Button) findViewById(R.id.button);
Recieve =(TextView) findViewById(R.id.textView2);
two.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
sendMessage();
}
});
}
public void sendMessage() {
Intent intent = new Intent(this, Activity2.class);
startActivityForResult(intent, REQUEST_CODE);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_CODE && resultCode == RESULT_OK) {
String requiredValue = data.getStringExtra("Key");
Recieve.setText(requiredValue);
}
}
And that is Activity2 code :
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_2);
send();
}
public void send(){
String message ="hello";
Intent intent = new Intent();
intent.putExtra("key", message);
setResult(RESULT_OK, intent);
finish();
}
}
答案 0 :(得分:0)
在send()
中,您使用"key"
小写k
。
在onActivityResult()
中,您使用"Key"
,资本为K
。
这些不匹配,这就是为什么你没有得到你的答复。
也许您应该在一个地方定义您的密钥,作为常量(final static
),并在两个活动中使用相同的常量定义。