如何在另一个活动中传递来自不同意图的不同值。即
活动A:
ButtonA .onclick {
intent.putExtra("name", screenname);
intent.putExtra("email", description);
intent.putExtra("pic", twitterImage);
startActivity(intent);
}
ButtonB。的onClick {
intent.putExtra("anothervalue", json_object.toString())
}
活动B:
Intent intent = getIntent();
String getValue = intent.getStringExtra("value from any of the button clicked")
答案 0 :(得分:2)
虽然David Rauca回答基本上是正确的,但您可能会面临NullPointerException
。
getIntent().getStringExtra("firstvalue")
会导致NPE。
您应该检查值是否存在。
if(getIntent().hasExtra("firstvalue")) {
String firstvalue = getIntent().getStringExtra("firstvalue");
}
答案 1 :(得分:1)
@Mandeep的回答是正确的。但是如果你有更多来自活动的价值,那么这就是解决方案。感谢Mandeep
Intent i = getIntent();
String getValue1,getValue2,getValue3;
if(i.hasExtra("AFirstValue") && i.hasExtra("ASecondValue") && i.hasExtra("AThirdValue")){
getValue1 = i.getStringExtra("AFirstvalue");
getValue2 = i.getStringExtra("ASecondValue");
getValue3 = i.getStringExtra("AThirdValue");
}
if(i.hasExtra("anotherFirstvalue") && i.hasExtra("anotherSecondvalue") && i.hasExtra("anotherThirdvalue")){
getValue1 = i.getStringExtra("anotherFirstvalue");
getValue2 = i.getStringExtra("anotherSecondvalue");
getValue3 = i.getStringExtra("anotherThirdvalue");
}
答案 2 :(得分:0)
String getValue = intent.getStringExtra("firstvalue") // in order to get the first value that was set when user clicked on buttonA
或
String getValue = intent.getStringExtra("anothervalue") // in order to get the the value that was set when user clicked on buttonB
答案 3 :(得分:0)
Activity B
代码应该是这样的
Intent intent = getIntent();
String firstvalue = intent.getStringExtra("firstvalue");
String anothervalue = intent.getStringExtra("anothervalue");
if(firstvalue != null)
// called from Button A click
else if(secondvalue != null)
// called from Button B click
答案 4 :(得分:0)
Intent intent = getIntent();
String getValue = null;
if(intent.hasExtra("firstvalue")){
getValue = intent.getStringExtra("firstvalue");
}
if(intent.hasExtra("anothervalue")){
getValue = intent.getStringExtra("anothervalue");
}