I have been using Robospice to complete my network operations. In Robospice, they recommend that the SpiceService
(an extension of the android service class) be started in a fragment's onAttach()
method:
Other solution is to use second approach which is getting own
SpiceManager
inFragment.onAttach(...)
and releasing theSpiceManager
inFragment.onDetach()
https://github.com/stephanenicolas/robospice/wiki/Advanced-RoboSpice-Usages-and-FAQ
So I have been trying to do so but I see that sometimes the fragment's onAttach()
method is not called at all when you use fragments with a Viewpager
. I have created a github repo showing this problem:
https://github.com/Winghin2517/ViewPagerFragmentSample
In my github repo, I have 3 fragments and one viewpager. I'm logging when each fragment's onAttach
method is called and the results are erratic. Closing the app and restarting it produces this sometimes:
12-27 12:55:17.085 8831-8831/? E/Fragment2: onAttachedActivity
12-27 12:55:17.085 8831-8831/? E/Fragment2: onAttachedContext
12-27 12:55:17.097 8831-8831/? E/Fragment1: onAttachedActivity
12-27 12:55:17.097 8831-8831/? E/Fragment1: onAttachedContext
or just this:
12-27 12:56:30.018 8864-8864/? E/Fragment2: onAttachedActivity
12-27 12:56:30.022 8864-8864/? E/Fragment2: onAttachedContext
I'm logging both when public void onAttach(Context context)
is called for api 23 (this shows in the log as "onAttachedContext") and when public void onAttach(Activity activity)
is called for api below 23 (this shows in the log as "onAttachedActivity") even though I'm using the support Library 23.
It is seldom that all 3 fragment's onAttach
methods are called each time the app starts which means that my robospice service for network operations are also not started occasionally leaving the app hanging.
How do I get the app to call all the onAttach methods for all fragments on start?