I add fragments to my activity programatically in a for loop like this:
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
for(int i = 0; i<bl.size(); i++)
{ft.add(R.id.linearlayout, new BackflowDeviceFragment(), Integer.toString(i));}//for
ft.commit();
fm.executePendingTransactions();
I then attempt to obtain references to these fragments like so:
BackflowDeviceFragment frag = (BackflowDeviceFragment) fm.findFragmentByTag(Integer.toString(i));
I successfully get a reference, however whenever I try to operate on any of the fragments' UI elements they are found to be null. So I did some debugging and found that the public methods that I created to operate on the UI elements are being called before the onCreateView() method where I set up all the UI stuff.
When is onCreateView() called? How can I successfully work with these UI elements?
According to Fragment Documentation, onCreateView() :
This will be called between onCreate(Bundle) and onActivityCreated(Bundle).
so you shouldn't reference your UI Elements before Initializing.
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