我有一个活动是一个容器几个片段。一个片段的开始另一个活动,并从第二活动欲一些数据发送到所述片段之一。我该怎么办呢?基本上在第一活动保持超出第二一个和EditViews中的一个将一个新的值来更新所述第二活动关闭时。我可能已经使用的意图,但我怎么能发送,如果该活动已经开始了吗?谢谢你。
I have an activity which is a container for several fragments. One of the fragments starts another activity and from the second activity I want to send some data to one of the fragments. How can I do that? Basically the first activity stays beyond the second one and one of the EditViews will be updated with a new value when the second activity closes. I could've used an intent but how can I send it if the activity is already started? Thank you.
您需要使用 startActivityForResult()
来开始你的第二个活动。在你的第二个活动,你完成它之前,你需要将数据添加到包通过这个来的意图,然后将结果的意图。
You would need to start your second activity using startActivityForResult()
. In your second activity before you finish it, you need to add the data to a bundle pass this to an intent and then set the result to the intent.
Bundle bundle = new Bundle();
bundle.putString("myData", "myValue");
Intent intent = new Intent();
intent.putExtra(bundle);
setResult(intent, 0);
finish();
然后在活动1应该有一个 onactivityresult
方法,从目的检索值,并将它放在你想在你的片段
And then in activity 1 there should be an onactivityresult
method which retrieves the value from the intent and sets it where you want in your fragment
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Bundle bundle = data.getData();
string value = bundle.getString("myData");
}
我不知道如果我有完全正确的,在我的头顶记住它,但应该足以让你开始,我认为。
I'm not sure if I have it exactly right as remembering it at the top of my head but should be enough to get you started I think.