Is there a way to send a bundle to a fragment without actually putting that fragment into a container? I ask because I want to take a few Strings from an EditTexts input field and then call them in another fragment at a later time.
I have code like this
Bundle bundle = new Bundle();
Fragment fragment = new Assessment_Fragment();
bundle.putString("company", companyName);
bundle.putString("project", projectName);
fragment.setArguments(bundle);
getFragmentManager().beginTransaction()
.setCustomAnimations(android.R.animator.fade_in, android.R.animator.fade_out)
.replace(R.id.header_fragment_container, fragment).commit();
In this code I pass the bundle with the transaction but I also replace the existing fragment in the "header_fragment_container". I don't want to do that. I just want to pass the bundled data to the specified fragment. When that fragment is eventually called I will receive the bundle. Is this possible?
Is this possible?
Sure. Just delete the last Java statement (getFragmentManager()....).
Now, as to whether it is sensible for you to create a fragment, then never add it to the fragment manager (either with a UI or not), that's another matter. Java supports lots of classes; Fragment is just one of them. If you need a generic data container, choose something lighter weight, such as just the Bundle itself.
Related
i need an approach how to handle this topic:
i have create a Mainactivity (A) and a Tabbed Activity (B) that contains 3 Tabs (Fragment One, Fragment Two and Fragment Tree)
how to send a string from the Mainactivity to the Fragment One without starting the Tabbed Activity.
when i use bundle
let say: in MainActivity
Bundle bundle=new Bundle();
bundle.putString("key", editText.getText().toString());
BlankFragmentTab1 fragment = new BlankFragmentTab1();
fragment.setArguments(bundle);
getSupportFragmentManager().beginTransaction().replace(R.id.frag,fragment).commit();
and inside Fragment One i get the String with getarguments.getString("key")
i get a nullpointerexception.
the approach works only by the communication between the Fragments and its container Activity.
thank you very much for your help
LieForBananas is correct.
You can not interact with your FragmentOne without calling its host activity.
However you can use SharedPreferences instead of Bundle for retrieving data.
My parent activity is launching another activity and needs its fragments to update when the results are returned via onActivityResult. Should I simply implement methods in each fragment to pass the data into them, or is there a different way this should be done?
Thanks!
Make your fragment that should receive data implement an interface with a suiting method.
In the activity when the result is returned from onActivityResult, use the getFragmentManager().findFragmentByTag("yourtag") method to find the fragment. Cast it to the interface and call the method is has.
Make sure to check if the fragment actually exists after calling findFragmentByTag.
If the fragment is the one making the startActivityForResult call, the activity gets the first shot at handling the result. This makes sense when you consider the modularity of fragments. Once you implement super.onActivityResult for all unhandled results, the fragment got a shot at handling the result.
To get the result in your fragment make sure you call : startActivityForResult(intent,123); instead of getActivity().startActivityForResult(intent,123); inside your fragment.
I hope this will solve your problem.
Someday I will have to solve your issue or similar. Per my understanding, you'll have to implement an interface between Activity and a Fragment. The link is at Communicating with Other Fragments . The webpage seems clear unlike other Google pages. A snippet of code:
ArticleFragment newFragment = new ArticleFragment();
Bundle args = new Bundle();
args.putInt(ArticleFragment.ARG_POSITION, position);
newFragment.setArguments(args);
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
// Replace whatever is in the fragment_container view with this fragment,
// and add the transaction to the back stack so the user can navigate back
transaction.replace(R.id.fragment_container, newFragment);
transaction.addToBackStack(null);
// Commit the transaction
transaction.commit();
Almost always, class FragmentTransaction is involved in managing data. Note how Bundle is used to pass data, also to get data from the client side.
Have fun, keep us posted.
Is that compulsory to set data using bundle and set argument?
What is wrong Here?
MyFragment frag = new MyFragment ()
frag.setData(mSchoolData);
//add to back stack stuff.
by using the setArgs() you will ensure that this Fragment can be recreated due to lifecycle event...while by passing arguments with your own setters it may not work properly under certain circustances. That is why it is absolutely recommended to either uset Args OR use Intent extras, these will always be automatically provided by the system if the fragment gets recreated.
I am still new to Android programming, i did search for checking the difference between Bundle in Activity and in Fragment and what are the use cases of these.
Please give me the difference with exact use case.
Thanks for your help :)
It is a different bundle.
When you create activity you get system or custom bundle (with save data).
When you create fragment you can put same date from activity (ex same id) but this will be a new bundle.
If you want same data in frament like super.onSaveInstanceState(savedInstanceState); in activity, you need to return this bundle to activity, and for restoring you must parse this data in activity and put to fragment.
I currently have an app that relies heavily on Intents and the extras given to them before starting the activity. The extras are used when calling a webservice which in turn supplies the content that needs to be shown
I am trying to convert that model, to one where I have a static Fragment (lets call it Player) at the bottom of my screen, and another Fragment (lets call it Content) above it which will show the main content. By selecting options on the main screen other content will be shown by replacing the Content Fragment.
But, these new Fragments are currently the Intents that rely on the extras so heavily.
Is there a way to replace a Fragment by a new one, but still be able to add extras to it?
If so, let's say I have the following piece of code:
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.fragment_holder, new MusicAlbumList(), "albumlist");
ft.commit();
How would I add the extras to the MusicAlbumList?
If that's not possible, how will I get the data that's currently being passed through extras into my new Fragment before it force closes due to missing essential data?
Or you can do this
MusicAlbumList fragment = new MusicAlbumList();
Bundle args = new Bundle();
args.putString("StringName","Value here");
fragment.setArguments(args);
then do your replace stuff. Then in the fragments onStart or onCreate call this.getArguments(); to pull the bundle, then get your extras out of there.
Change the constructor of MusicAlbumList from default constructor to one with arguments like new MusicAlbumList(int arg1, ...) and pass the values you want to set through the constructor