发送字符串从活动到另一个活动一个片段 [英] Send String from an Activity to a Fragment of another Activity
问题描述
我有两个活动(A和B)和一个片f 该片段F被包含在b活动 我想从活动一字符串发送到片f 如何才能做到这一点? 谢谢!
I have two Activities (A and B) and a Fragment F The Fragment F is contained in the Activity B I'd like to send Strings from Activity A to Fragment F How can do that? Thanks!
推荐答案
首先,你实际上发送串到你的活动B.例如:
First, you'll actually send that string to your activity B. For example:
Intent intent = new Intent(this, YourActivityClass.class);
intent.putExtra("myString", "this is your string");
startActivity(intent);
然后再读取该字符串从你的活动B和执行片段交易之前注入到您的片段。例如:
then later read that string from your activity B and inject into your fragment before executing the fragment-transaction. For example:
Bundle args = new Bundle();
args.putString("myString", getIntent().getExtras().getString("myString"))
yourFragment.setArguments(args);
后来,使用 getArguments()
在你的片段来检索包。
Later, use getArguments()
in your fragment to retrieve that bundle.
或者,使用下面的在你的片段直接访问该活动意图并获取您需要的值:
Or alternatively, use the following in your fragment to directly access the activity intent and fetch your required value:
String str = getActivity().getIntent().getStringExtra("myString");
有关详细信息,请参阅这。
For more info, read this.
这篇关于发送字符串从活动到另一个活动一个片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!