更新片段UI与活动传递的数据 [英] Update Fragment UI with data passed from Activity
问题描述
我的要求:
我的 MainActivity
从其他应用程序接收数据。 MainActivity
被列为共享
。
现在,我需要在 MainActivity
将此数据传递给片段
和更新片段的
的TextView
。
在 MainActivity.java:我在这里处理接收到的数据(这是一个URL)的 handleSendText
法
如果(Intent.ACTION_SEND.equals(行动)及和放大器;类型!= NULL){
如果(text / plain的.equals(类型)){
handleSendText(意向); //处理文本发送
}
}
}
在 handleSendText
,我想创建一个包,并将该数据传递给我的片段。
无效handleSendText(意向意图){
字符串sharedText = intent.getStringExtra(Intent.EXTRA_TEXT);
如果(sharedText!= NULL){ 束束=新包();
bundle.putString(URL,sharedText); //设置Fragmentclass参数
AddTab fragobj =新AddTab(); // AddTab()是我的片段类的名字
fragobj.setArguments(包);
}
在片段类:在其onCreateView()
公开查看onCreateView(LayoutInflater充气器,容器的ViewGroup,
捆绑savedInstanceState){
...
//一些code 捆绑包= this.getArguments(); 如果(捆绑!= NULL){ 字符串sharedUrl = getArguments()的getString(URL)。 textBox.setText(sharedUrl); inflater.inflate(R.layout.fragment_add,集装箱,FALSE);
//更新UI,以反映变化,我想的
//上面一行。这样对吗?
}
1)的问题是控制从来都不是如果
循环,这意味着捆绑内到达code>总是返回
NULL
。
2)此外,如果我没有收到来自其他应用程序的数据。我要离开我的 EDITTEXT
空的,所以我必须执行此check.How我能做到这一点?
3)此外,从 setArgument
的<一个href=\"http://developer.android.com/reference/android/app/Fragment.html#setArguments(android.os.Bundle)\"相对=nofollow>文档,我才知道,之前该片段已被连接到它的活动应该叫。然后,我怎么能体现在片段的UI的变化?
公共无效setArguments(捆绑参数)
供应结构参数此片段。之前,该片段已被连接到它的活动这只能调用;也就是说,你应该叫
它构建片段之后。提供的参数
这里将在片段破坏和创造被保留。
块引用>解决方案我假设你已经通过添加
fragmentManager.add片段(..)
这样的片段已经在布局如果这是真的,那么你的
AddTab fragobj =新AddTab()
是没有意义的,因为碎片已经存在。您需要或者存储在活动这个片段的引用,或使用
fragmentManager.findFragmentById()
或findFragmentByTag()
。然后按照赛义德说什么
My Requirement :
My
MainActivity
receives data from other app.MainActivity
is listed asshareable
.Now, I need to pass this data to a
fragment
inMainActivity
and update thefragment's
textview
.In MainActivity.java : here I'm handling the received data (which is a URL) in
handleSendText
method.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.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()
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 meansbundle
is always returningNULL
.2) Moreover, if I don't receive the data from other app. I want to leave my
editText
empty, so I have to perform this check.How can I make that happen?3) Moreover, from
setArgument
's documentation, I learnt that it should be called before the fragment has been attached to its activity. Then how can I reflect the changes in the Fragment's UI?public void setArguments (Bundle args)
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.
解决方案I'm assuming you already added the fragment via
fragmentManager.add(..)
so the fragment is already on the layout.If this is true, then your
AddTab fragobj = new AddTab()
makes no sense since the Fragment already exists.You need to either store a reference to this fragment in the activity, or use
fragmentManager.findFragmentById()
orfindFragmentByTag()
.Then follow what Saeed says
这篇关于更新片段UI与活动传递的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!