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..
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.
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!!
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));
Related
I am trying to build simple movies name list in recyclerview inside drawer layout when I click any movie name show me the detail of movie like an image, textView in another fragment. How to implement a bundle for sending data to fragment from an activity.This is my main activity
You can send data from your activity to fragment like this:
In your MainActivity , add fragment when you want to open detail fragment
addFragment(R.id.frame_container,MovieDetailFragment.getInstance(/*your data*/),tag_name);
//then commit
In your Movie Detail Fragment,
public static MovieDetailFragment(//your data//){
MovieDetailFragment detailFragment new MovieDetailFragment();
Bundle bundle = new Bundle();
bundle.putString(key,//yourdata//);//you can use any type you want to put in bundle
detailFragment.setArguments(bundle);
return movieDetailFragment;
}
and in your MovieDetailFragment's onCreateView() , you can get your data from bundle like this:-
if(getArguments()!=null){
String data = getArguments().getString(key);
}
I hope it will solve your query!!
Step 1: Passing the data from activity to fragment,
Bundle bundle = new Bundle();
bundle.putString("params", "My String data");
set MyFragment Arguments
MyFragment myObj = new MyFragment();
myObj.setArguments(bundle);
Step 2: Receiving the data to the fragment,
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam1 = getArguments().getString("params");
}
}
Hello I'm trying to pass a String value from a fragment to another one. I have a single Activity.
The thing that I'm trying to do is when a listview item is pressed send the value to the another fragment, load the fragment (This I had made with this code):
Fragment cambiarFragment = new FragmentInterurbanos();
Bundle args = new Bundle();
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction
.replace(R.id.container, cambiarFragment)
.addToBackStack(null)
.setCustomAnimations(R.anim.slide_in_left, R.anim.slide_out_right);
// Commit the transaction
transaction.commit();
And then retrieve the String and update a WebView placed in the new fragment (This method cannot be launched when the user select in NavigationDrawer this section (Fragment).
Thanks for your help in advance!
There are many ways you can achieve this... you could pass the string to the new Fragment through a Bundle like this:
Bundle bundle = new Bundle();
bundle.putString(key, value);
fragment.setArguments(bundle);
or maybe have a DataController class where you store the string and the new fragment retrieves it.
I have an Activity with two fragments and I need to pass a string from FragmentA to FragmentB.
To pass the data, I have this in my FragmentA:
Intent intent = new Intent(getActivity(), FragmentB.class);
intent.putExtra("name", "Mark");
startActivity(intent);
And to get the data, I did this in FragmentB
Intent intent = getActivity().getIntent();
Bundle b = intent.getExtras();
if(b!=null)
{
String name =(String) b.get("name");
nameTextView.setText(name);
}
But this is not working. Is there a specific way to pass a string from one fragment to another fragment?
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.
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.
to pass data between Fragments you can use setArguments(Bundle b). For instance:
public class FragmentA extends Fragment {
public static FragmentA newInstance(String name) {
Bundle bundle = new Bundle();
bundle.putString("name", name);
FragmentA f = new FragmentA();
f.setArguments(bundle);
return f;
}
}
You can do something like below,
Fragment fr=new friendfragment();
FragmentManager fm=getFragmentManager();
android.app.FragmentTransaction ft=fm.beginTransaction();
Bundle args = new Bundle();
args.putString("CID", "your value");
fr.setArguments(args);
ft.replace(R.id.content_frame, fr);
ft.commit();
To receive the data do the following,
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
String strtext = getArguments().getString("CID");
return inflater.inflate(R.layout.fragment, container, false);
}
Code in FragmentActivity 1 fragment A:
fb is the instance of the bean I created and ID is the parameter
Intent intent = new Intent(getContext(), FragmentActivity2.class);
intent.putExtra("id", fb.ID);
startActivity(intent);
Code in FragmentActivity 2 fragment Any:
fragmentA= (FragmentActivity2) getActivity();
String cust_id = fragmentA.getIntent().getExtras().getString("id");
I want to pass the single Image from one Fragment to another Fragment, where the Images are placed in GridView of one Fragment and have to set them as background of FrameLayout contained in another Fragment,,
Thanks,,
Once Try This , call this at setOnItemClickListener
Fragment fragment = new ImageFullScreen();
Bundle bun = new Bundle();
bun.putInt("selected_image", position);
fragment.setArguments(bun);
if (fragment != null) {
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction().add(R.id.frame_container,fragment).addToBackStack(null).commit();
}
add this at second Fragment ,
Bundle bundle = this.getArguments();
int position = bundle.getInt("selected_image");;
Toast.makeText(getActivity(), ""+position, 1).show();
Drawable image =getResources().getDrawable(WallPaperoneFragment.imagefield.get(position));imageView.setImageDrawable(image);
pass image url fragment to another fragment as argument..
pass as
`
another_Fragment fr = new another_Fragment();
Bundle bun = new Bundle();
bun.putString("selected_image", image_url);
fr.setArguments(bun);`
and get value as String image_url=getArguments().getString("selected_image"); in anothe_fragment`
or use interface to communicate
How can we send data from actvity to fragment? The Fragments are configured to actvity by using FragmentPagerAdapter.
Regards
mini.
You can pass a bundle to the fragment on creation with setArguments.
You can create methods to set the data on the Fragment class.
You can perform this by using Bundle
Send data from the activity (or fragment) :
int a = 5;
Bundle args = new Bundle();
args.putInt("INT_DATA_TAG", a);
Fragment fragment = Fragment.newInstance(args);
//Making fragment transaction
Retrieve data in the fragment
int a;
public static Fragment newInstance(Bundle args) {
a = args.getInt("INT_DATA_TAG"); //use a constant for the tag
return new Fragment();
}