从Android的另一个活动将数据发送到片段 [英] send data to fragment from another activity in android

查看:138
本文介绍了从Android的另一个活动将数据发送到片段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个活动是一个容器几个片段。一个片段的开始另一个活动,并从第二活动欲一些数据发送到所述片段之一。我该怎么办呢?基本上在第一活动保持超出第二一个和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.

这篇关于从Android的另一个活动将数据发送到片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆