如何在android中将子活动的结果返回给父母?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10582314/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get back the result from Child activity to Parent in android?
提问by SANDHYA
I'am starting a child activity on click of a button from Parent. And i'am calculating some result (of type string) in child activity and finishing the child to come back to Parent. Is there any better way to get that result in Parent using intents or extras? (I can get that result in Parent by making the result variable as public & static in Child) Please help me. I'am a newbie to android development.
我正在点击父级按钮开始一个子活动。我正在计算子活动中的一些结果(字符串类型)并完成孩子回到父级。有没有更好的方法可以使用意图或附加功能在 Parent 中获得该结果?(我可以通过在 Child 中将结果变量设置为 public 和 static 来在 Parent 中获得该结果)请帮助我。我是android开发的新手。
startActivityForResult(new Intent(ParentActivity.this, ChildActivity.class), ACTIVITY_CONSTANT);
What should i write in onActivityResult() of Parent?
我应该在 Parent 的 onActivityResult() 中写什么?
回答by Paresh Mayani
Instead of startActivityForResult(new Intent(ParentActivity.this, ChildActivity.class), ACTIVITY_CONSTANT);
代替 startActivityForResult(new Intent(ParentActivity.this, ChildActivity.class), ACTIVITY_CONSTANT);
You can use putExtras() method to pass values between activities:
您可以使用 putExtras() 方法在活动之间传递值:
In Child Activity:
在儿童活动中:
Intent data = new Intent();
data.putExtra("myData1", "Data 1 value");
data.putExtra("myData2", "Data 2 value");
// Activity finished ok, return the data
setResult(RESULT_OK, data);
finish();
And in Parent activity, you can override onActivityResult() and inside that you can have Intent paramater and from the Intent parameter of this method you can retrieve the extra values passed from the child activity, such as intent.getStringExtra or intent.getSerializableExtra.
在父活动中,您可以覆盖 onActivityResult() 并在其中拥有 Intent 参数,并且从该方法的 Intent 参数中,您可以检索从子活动传递的额外值,例如 intent.getStringExtra 或 intent.getSerializableExtra。
for example:
例如:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK && requestCode == REQUEST_CODE) {
if (data.hasExtra("myData1")) {
Toast.makeText(this, data.getExtras().getString("myData1"),
Toast.LENGTH_SHORT).show();
}
}
}
回答by Pankaj Talaviya
In Parent Activity
在父活动中
Intent intent = new Intent(getApplicationContext(), yourChildActivity.class);
intent.putExtra("key", "value");
startActivityForResult(intent, ACTIVITY_CONSTANT);
in child activity to sent back result of your parent activity through
在子活动中通过发回您的父活动的结果
Intent data = new Intent();
data.putExtra("key1", "value1");
data.putExtra("key2", "value2");
// Activity finished return ok, return the data
setResult(RESULT_OK, data);
finish();
and get child activity result information in your parent activity
并在您的父活动中获取子活动结果信息
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK && requestCode == REQUEST_CODE) {
if (data.hasExtra("key1") && data.hasExtra("key2")) {
Toast.makeText(
this,
"Your reult is : "data.getExtras().getString("key1") + " " + data.getExtras().getString("key2"),
Toast.LENGTH_SHORT).show();
}
}
}