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.
I am sending string url from this Fragment to first fragment.
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.
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..
When working with child fragments, your parent fragment and its child fragments might need to share data with each other. To share data between these fragments, use the parent fragment as the ViewModel scope.
Step 1 − Create a new project in Android Studio, go to File ⇉ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml. Step 3 − Create two FragmentActivity and add the codes which are given below.
In one fragment activity, call a method and pass a variable to the main activity. From the main activity you can send it to your other fragment activity if you'd like. Show activity on this post. You can also use SharedPreferences to save some string and after return back to the first fragment load it and clear.
Use Bundle to send 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:
//Retrieve the value String value = getArguments().getString("YourKey");
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With