Android 如何将数据从一个 Fragment 发送到另一个 Fragment?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24555417/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to send data from one Fragment to another Fragment?
提问by Muhidul Hassan
Hi I know there are answers of this question. I have tried all of them but it is not working in my app. I am developing an app that has 3 Fragment activity. First fragment shows a website, second fragment has listview and third Fragment has another listview. Now I want to send URL from third fragment to first fragment when user clicks on listitem..This is what I have done.
嗨,我知道这个问题有答案。我已经尝试了所有这些,但它在我的应用程序中不起作用。我正在开发一个具有 3 个 Fragment 活动的应用程序。第一个片段显示一个网站,第二个片段有列表视图,第三个片段有另一个列表视图。现在我想在用户点击 listitem 时将 URL 从第三个片段发送到第一个片段。这就是我所做的。
I am sending string url from this Fragment to first fragment.
我正在将此片段中的字符串 url 发送到第一个片段。
list.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapter, View view, int position,
long id) {
FragmentC fragment = new FragmentC();
final Bundle bundle = new Bundle();
bundle.putString("position", "http://www.facebook.com"); fragment.setArguments(bundle);}
});
This is first fragment where I need the url and Want to show in the webview.
这是我需要 url 并希望在 webview 中显示的第一个片段。
String url="http://www.hotelsearcher.net/";
Bundle args = getArguments();
if (args != null){
url = args.getString("position");
}
WebView webView= (WebView) V.findViewById(R.id.webView1);
WebSettings webViewSettings = webView.getSettings();
webViewSettings.setJavaScriptCanOpenWindowsAutomatically(true);
webViewSettings.setJavaScriptEnabled(true);
webViewSettings.setPluginState(PluginState.ON);
webView.loadUrl(url);
When I click on list item I don't see anything . It does not redirect me to the first fragment.Please help me..
当我点击列表项时,我什么也没看到。它不会将我重定向到第一个片段。请帮助我..
回答by Jo?o Marcos
Use Bundleto send String
:
使用捆绑发送String
:
//Put the value
YourNewFragment ldf = new YourNewFragment ();
Bundle args = new Bundle();
args.putString("YourKey", "YourValue");
ldf.setArguments(args);
//Inflate the fragment
getFragmentManager().beginTransaction().add(R.id.container, ldf).commit();
In onCreateView
of the new Fragment:
在onCreateView
新片段中:
//Retrieve the value
String value = getArguments().getString("YourKey");
回答by ViJay
1.If fragments are hosted by same activity- You cannot cast an intent to Fragment. Fragment acts as a part of Activity, it is not an activity by itself. So to share a string between fragments you can declare a static String in Activity. Access that string from Fragment A to set the value and Get the string value in fragment B.
1.如果片段由同一活动托管 - 您不能将意图投射到片段。Fragment 是 Activity 的一部分,它本身并不是一个 Activity。因此,要在片段之间共享字符串,您可以在 Activity 中声明一个静态字符串。从片段 A 访问该字符串以设置值并获取片段 B 中的字符串值。
2.Both fragments are hosted by different Activities- Then you can use putExtra to pass a string from Fragment A of Activity A to Activity B. Store that string in Activity B and use it in Fragment B.
2.两个片段都由不同的活动托管 - 然后您可以使用 putExtra 将字符串从活动 A 的片段 A 传递到活动 B。将该字符串存储在活动 B 中并在片段 B 中使用它。
回答by Alfaplus
You have to attach your bundle to your fragment.
你必须将你的包附加到你的片段上。
fragment.setArguments(bundle);
and after that change or replace the new fragment.
然后更改或替换新片段。
You have to be sure that the String is in the new Fragment. Debugging!!
您必须确保字符串在新的片段中。调试!!
回答by Narender Gusain
You can send data by two ways First when you want to start that fragment while sending data
您可以通过两种方式发送数据首先当您想在发送数据时启动该片段时
SecondFragment ldf = new SecondFragment ();
Bundle args = new Bundle();
args.putString("YourKey", "YourValue");
ldf.setArguments(args);
//Inflate the fragment
getFragmentManager().beginTransaction().add(R.id.container, ldf).commit();
Second, when you want to send data without open your fragment
其次,当您想在不打开片段的情况下发送数据时
FragmentTransaction ft = mFragmentManager.beginTransaction();
ft.add(R.id.fragContainer1, new ModelListFragment(), FRAG_MODEL_LIST);
ft.add(R.id.fragContainer2, new TrimListFragment(), FRAG_TRIM_LIST);
ft.commit();
Fragment fragment = mFragmentManager.findFragmentByTag(MainActivity.FRAG_MODEL_LIST);
Log.d("MY", "found fragment: " + (fragment != null));