[英]Update Fragment UI with data passed from Activity
My Requirement : 我的要求:
My MainActivity
receives data from other app. 我的
MainActivity
从其他应用程序接收数据。 MainActivity
is listed as shareable
. MainActivity
被列为shareable
。
Now, I need to pass this data to a fragment
in MainActivity
and update the fragment's
textview
. 现在,我需要将此数据传递给
MainActivity
一个fragment
,并更新该fragment's
textview
。
In MainActivity.java : here I'm handling the received data (which is a URL) in handleSendText
method. 在MainActivity.java中:在这里,我正在处理
handleSendText
方法中接收到的数据(这是一个URL)。
if (Intent.ACTION_SEND.equals(action) && type != null) {
if ("text/plain".equals(type)) {
handleSendText(intent); // Handle text being sent
}
}
}
In handleSendText
, I'm trying to create a bundle and pass that data to my fragment. 在
handleSendText
,我试图创建一个包并将该数据传递给我的片段。
void handleSendText(Intent intent) {
String sharedText = intent.getStringExtra(Intent.EXTRA_TEXT);
if (sharedText != null) {
Bundle bundle = new Bundle();
bundle.putString("url", sharedText);
// set Fragmentclass Arguments
AddTab fragobj = new AddTab(); //AddTab() is my Fragment class's name
fragobj.setArguments(bundle);
}
In Fragment class : In its onCreateView() 在Fragment类中:在其onCreateView()中
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
...
//some code
Bundle bundle = this.getArguments();
if(bundle!=null) {
String sharedUrl = getArguments().getString("url");
textBox.setText(sharedUrl);
inflater.inflate(R.layout.fragment_add, container, false);
// to update the UI to reflect changes, I'm trying the
// above line. Is it right?
}
1) The problem is the control is never reaching inside of the if
loop, which means bundle
is always returning NULL
. 1)问题是控件永远不会到达
if
循环内部,这意味着bundle
总是返回NULL
。
2) Moreover, if I don't receive the data from other app. 2)而且,如果我没有从其他应用程序接收数据。 I want to leave my
editText
empty, so I have to perform this check.How can I make that happen? 我想将我的
editText
留editText
空白,所以我必须执行此检查。如何实现?
3) Moreover, from setArgument
's documentation , I learnt that it should be called before the fragment has been attached to its activity. 3)此外,从
setArgument
的文档中 ,我了解到应该在将片段附加到其活动之前调用它。 Then how can I reflect the changes in the Fragment's UI? 那我该如何反映片段的UI中的更改?
public void setArguments (Bundle args)
公共无效setArguments(捆绑参数)
Supply the construction arguments for this fragment.
提供此片段的构造参数。 This can only be called before the fragment has been attached to its activity;
这只能在片段附加到其活动之前被调用; that is, you should call it immediately after constructing the fragment.
也就是说,您应该在构建片段后立即调用它。 The arguments supplied here will be retained across fragment destroy and creation.
此处提供的参数将在片段销毁和创建过程中保留。
Just add a proper method in your fragment, something like this: 只需在片段中添加适当的方法,如下所示:
public void receiveURL(String input){
//to handle what you want to pass to the fragment
}
And in your mainActivity call you can do something like this: 在mainActivity调用中,您可以执行以下操作:
YourFragment fragment = (YourFragment)getSupportFragmentManager.findFragmentById(R.id.your_fragment);
fragment.receive(sharedUrl);
I'm assuming you already added the fragment via fragmentManager.add(..)
so the fragment is already on the layout. 我假设您已经通过
fragmentManager.add(..)
添加了片段,因此该片段已经在布局上。
If this is true, then your AddTab fragobj = new AddTab()
makes no sense since the Fragment already exists. 如果是这样,则您的
AddTab fragobj = new AddTab()
没有任何意义,因为Fragment已经存在。
You need to either store a reference to this fragment in the activity, or use fragmentManager.findFragmentById()
or findFragmentByTag()
. 您需要在活动中存储对此片段的引用,或者使用
fragmentManager.findFragmentById()
或findFragmentByTag()
。
Then follow what Saeed says 然后按照赛义德所说的
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.