How to pass selected image from one fragment to another fragment - android

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

Related

How to pass the data fragment to other fragment activity in android studio

SectionFragment.class
Intent subjectDetail = new Intent(getContext(), GradeFragment.class);
ListGradeData clickItem = sectionList.get(position);
subjectDetail.putExtra(EXTRA_SECTION_ID, String.valueOf(clickItem.getSectionId()));
startActivity(subjectDetail);
GradeFragment.class
String studSectionId = getActivity().getIntent().getExtras().getString(EXTRA_SECTION_ID);
Toast.makeText(getContext(), studSectionId, Toast.LENGTH_LONG).show();
it doesn't display the studentSectionId. When I run the app it always crashed after I click the button
For fragment use FragmentManager to add/replace fragments. and create send data like
MyFragment myFragment = new MyFragment();
Bundle args = new Bundle();
args.putInt("someInt", someInt);
myFragment.setArguments(args);

How to use a bundle for sending data to fragment from an activity in Android Studio

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");
}
}

How to get element back on popBackStack( )

I have two fragments: in the first fragment I do it:
ritorno.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v)
{
manager.beginTransaction().replace(R.id.content_frame,new SearchFlight()).addToBackStack(null).commit();
}
});
and this works well. In the second I need to return to the first fragment, so I decide to do this:
String s = element.getText().toString(); //I need to return it
FragmentManager manager = getActivity().getSupportFragmentManager();
SearchFragment fragment = new SearchFragment();
manager.popBackStack();
I need to return to the first fragment this String. How could I do it?
Thanks
There are 3 ways
Static Variable : Define public static variable in first fragment, update its value in second fragment.
BroadcastReceiver
EventBus you can even find EventBus tutorial here
pass string to BR or EventBus and you can fetch it in first fragment by defining appropriate method or listener.
Try doing it with a bundle object.
String s = element.getText().toString(); //item to be returned
FragmentManager manager = getActivity().getSupportFragmentManager();
SearchFragment fragment = new SearchFragment();
Bundle bundle = new Bundle();
bundle.putString("key", "value");
fragment.setArguments(bundle);
manager.popBackStack();
And then, in your SearchFragment resolve the bundle like:
Bundle bundle = this.getArguments();
bundle.getString("key");
Do the resolution part of the bundle in your onCreateView of the SearchFragment. Do place a null check on the resolution part of the bundle. If the bundle is not resolved, then the getString("key") method will give a null pointer exception.

Pass data between two fragments in same activity and update WebView

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.

How to send data from one Fragment to another Fragment?

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));

Categories

Resources