If I declare a fragment in an XML layout, how do I pass it a Bundle?

Now that my Activity is just a wrapper around a Fragment that does the same work, how do I get that bundle to the Fragment if I declare the fragment in XML with the tag?

You can’t.

However, you are welcome to call findFragmentById() on your FragmentManager to retrieve the fragment post-inflation, then call some method on the fragment to associate data with it. While apparently that cannot be setArguments(), your fragment could arrange to hold onto the data itself past a configuration change by some other means (onSaveInstanceState(), setRetainInstance(true), etc.).

Leave a Comment